UITableViewCell 内容没有填满整个单元格 [英] UITableViewCell content not filling up whole cell
问题描述
我正在尝试在我的应用中实现自定义 UITableViewCells.我想向每个 UITableViewCell 添加一个带有多个子视图的 UIScrollView 并启用水平滚动.除了高度和分组之外,我大部分时间都能做到.在下面查看我的代码和屏幕截图.我无法将 UIScrollView 的高度更改为大于 44 点的数字.
I am trying to implement custom UITableViewCells in my app. I want to add a UIScrollView with several subviews to each UITableViewCell and enable horizontal scrolling. I was mostly able to do it except with the height and grouping. Check out my code and screenshot below. I am not able to change the UIScrollView's height to a number greater than 44 points.
如何将高度大于 44 的 UIScrollView 添加到我的自定义 UITableViewCell 中?
How can I add a UIScrollView, with a height greater than 44, to my custom 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 类
UITableCell class
#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
推荐答案
将 UIScrollView 的框架设置为 tableViewCell 的边界(或框架,我从不记得我的头顶):
Set the UIScrollView's frame to be the tableViewCell's bounds (or frame, I never remember off the top of my head):
scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
然后将其高度设置为 autoResize
Then set it's height to autoResize
scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
您不必在实际单元格中手动更改单元格的高度.高度应该全部在 TableViewController 的委托方法中管理.
You shouldn't have to manually change the cell's height within the actual cell. The height should all be managed in your TableViewController's delegate methods.
这篇关于UITableViewCell 内容没有填满整个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!