gpt4 book ai didi

ios - UITableViewCell 内容未填满整个单元格

转载 作者:行者123 更新时间:2023-11-29 03:45:17 25 4
gpt4 key购买 nike

我正在尝试在我的应用程序中实现自定义 UITableViewCells。我想向每个 UITableViewCell 添加一个带有多个 subview 的 UIScrollView 并启用水平滚动。除了高度和分组之外,我基本上都能做到。查看下面我的代码和屏幕截图。我无法将 UIScrollView 的高度更改为大于 44 点的数字。

如何将高度大于 44 的 UIScrollView 添加到我的自定义 UITableViewCell?

#import "HomeViewController.h"

@interface HomeViewController ()

@end

@implementation HomeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Home Scroll Cell";
homeScrollCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (!cell) {
cell = [[homeScrollCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}


NSLog(@"Cell: %f", cell.bounds.size.height);




return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100.0;
}

@end

UITableCell 类

#import "homeScrollCell.h"

@implementation homeScrollCell

//- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
//{
//
//}

-(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 100)];
NSInteger viewcount= 3;
for(int i = 0; i< viewcount; i++) {

CGFloat x = i * self.bounds.size.width;
HomeTodayView *view = [[HomeTodayView alloc] initWithFrame:CGRectMake(x, 0,self.bounds.size.width, self .bounds.size.height)];
view.backgroundColor = [UIColor greenColor];

[scrollView addSubview:view];

}
scrollView.contentSize = CGSizeMake(self.bounds.size.width *viewcount, 100);
scrollView.pagingEnabled = YES;
scrollView.bounces = NO;
[self addSubview:scrollView];


NSLog(@"Height: %f", self.bounds.size.height);
}
return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
// [super setSelected:selected animated:animated];

// Configure the view for the selected state
}

@end

Result

最佳答案

将 UIScrollView 的框架设置为 tableViewCell 的边界(或框架,我从来不记得):

scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];

然后将其高度设置为autoResize

scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight;

您不必手动更改实际单元格内的单元格高度。高度应该全部在 TableViewController 的委托(delegate)方法中进行管理。

关于ios - UITableViewCell 内容未填满整个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17836024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com