【发布时间】:2011-12-13 13:15:41
【问题描述】:
问题:
当我将rowHeight 设置为UITableView 时,单元格的高度不应该也改变吗?
以下是让我思考的情况:
我想为底部的每个表格视图单元格设置一个单独的行,此外,我想将它的行高设置为从44 到32。我想要的结果如下:
行高设置正确:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView setRowHeight:32.0f];
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
//...
}
// Even use the delegate of UITableView
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 32.0f;
}
但是,当我为单元格添加单独的行时,我遇到了一个问题:我想在单元格底部设置单独的行,所以我将y 的位置设置为cell.frame.size.height - 1.0f。不幸的是,结果如下图:
当我选择时,单元格改变如下:
1。选中第 1 行:
2。选中第 2 行:
3。选中第 3 行:
好像选中前cell的高度是44,选中后变成32。它们像卡片一样一张一张地重叠,对吧?奇怪!
主要代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
[cell.textLabel setFont:[UIFont fontWithName:@"Futura-Medium" size:15.0f]];
UIView * seperateLine = [[UIView alloc] initWithFrame:CGRectMake(10.0f, cell.frame.size.height - 1.0f, 300.0f, 1.0f)];
NSLog(@">>>>>>>>>>>>>>>> %f", cell.frame.size.height);
[seperateLine setBackgroundColor:[UIColor grayColor]];
[cell.contentView addSubview:seperateLine];
[seperateLine release];
}
//...
}
我尝试检查cell.frame.size.height,最后是44。然后,我换行
UIView * seperateLine = [[UIView alloc] initWithFrame:CGRectMake(10.0f, cell.frame.size.height - 1.0f, 300.0f, 1.0f)];
到
UIView * seperateLine = [[UIView alloc] initWithFrame:CGRectMake(10.0f, 31.0f, 300.0f, 1.0f)];
它就像上面显示的第一张图片一样。但是the cell's height was still 44,它们(我们完全看不到)仍然重叠。我所做的只是在y 位置为32 但其总高度为44 的位置添加了一个separate line。
那么您对此有何看法? :?
【问题讨论】:
-
默认行高是 44 磅是有原因的:这是人类手指的近似大小。如果你让行小于这个值,我希望你不要指望用户点击它们。
-
@PeterHosey 谢谢你的建议! :)
标签: iphone ios cocoa-touch uitableview uikit