我正在尝试在我的应用中实现自定义UITableViewCells。我想添加一个带有几个子视图的UIScrollView到每个UITableViewCell并启用水平滚动。除了身高和分组外,我几乎能够做到这一点。看看下面的代码和截图。我无法将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
答案 0 :(得分:1)
将UIScrollView的框架设置为tableViewCell的边界(或框架,我从不记得我的头脑):
scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
然后将其高度设置为autoResize
scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
您不必手动更改实际单元格中单元格的高度。高度应该都在TableViewController的委托方法中进行管理。
答案 1 :(得分:0)
您没有设置单元格的框架
您是否尝试过在线初始化HomeTodayView,以获取self.bounds.size.height的返回值?
也许你可以用另一个参数 - 框架为你的细胞制作另一个初始化器?像这样开始:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier frame:(CGRect)frame
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.frame = frame;
/* rest of the init method you already have goes here */
基本上,只需添加self.frame = frame;在&#39; if&#39;
的开头答案 2 :(得分:0)
UItableViewcell的初始大小为44.0f您在调用
后进行检查self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
你需要让你的scrollView高度可伸缩。
scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
这对我有帮助。
答案 3 :(得分:0)
只需使用
CGFloat x = i * self.bounds.size.width;
HomeTodayView *view = [[HomeTodayView alloc] initWithFrame:CGRectMake(x, 0,self.bounds.size.width, scrollView.bounds.size.height)];
view.backgroundColor = [UIColor greenColor];
或者用
替换整个代码- (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,self.bounds.size.height)];
[scrollView setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
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,scrollView.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];
// Initialization code
}
return self;
}
即使你不明白问题是什么,那么看看下面的行
你的问题来到这里
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 100)];
您正在将scrollView高度设置为常量值,因此当HeightForRowAtIndex路径调整单元格高度时,scrollView不会调整大小。
设置[scrollView setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
将允许scrollView在其容器调整大小时调整大小,并允许调整其内容的大小。