【发布时间】:2011-02-23 15:00:53
【问题描述】:
我将自己的UILabels 添加到UITableViewCell 的contentView 中,因为我需要对布局进行比默认 UITableViewCellStyles 提供的更多控制。本质上,我希望 detailLabel 优先于 textLabel,因此 textLabel 会被截断。
我的UITableViewController 中有以下代码:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * const kCellIdentifier = @"CustomCell";
UITableViewCell * cell =
[tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
UILabel * titleLabel, * dateLabel;
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:kCellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
titleLabel = [[[UILabel alloc] init] autorelease];
titleLabel.tag = kTitleLabelTag;
titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
dateLabel = [[[UILabel alloc] init] autorelease];
dateLabel.tag = kDateLabelTag;
dateLabel.textColor = [UIColor blueColor];
dateLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
[cell.contentView addSubview:titleLabel];
[cell.contentView addSubview:dateLabel];
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(UITableViewCell *)pCell
atIndexPath:(NSIndexPath *)pIndexPath
{
const float kHeight = 44.0, kLeftIndent = 8.0, kOverallWidth = 293.0,
kGap = 1.0;
UILabel * titleLabel, * dateLabel;
titleLabel = (UILabel *)[pCell.contentView viewWithTag:kTitleLabelTag];
dateLabel = (UILabel *)[pCell.contentView viewWithTag:kDateLabelTag];
NSString * dateText = @"9:39 AM";
// Calculate the size of dateLabel
CGSize dateSize = [dateText sizeWithFont:[dateLabel font]
constrainedToSize:CGSizeMake(kOverallWidth, kHeight)];
const float dateXPos = kOverallWidth - dateSize.width;
dateLabel.frame = CGRectMake(dateXPos, 0.0, dateSize.width, kHeight);
titleLabel.frame = CGRectMake(kLeftIndent, 0.0,
dateXPos - kLeftIndent - kGap, kHeight);
titleLabel.text = @"Some potentially very long text which will be wrapped.";
dateLabel.text = dateText;
pCell.contentView.backgroundColor = [UIColor purpleColor];
}
上面的代码产生了错误的结果。最初显示表格视图时,它看起来像 this image of the renderings 中的图 1)。
所以在所有dateLabels 的右侧有一个不需要的空白。 (紫色背景只是为了更好地了解正在发生的事情)
当像在图像中的 2) 那样向上拖动 tableview 时,它会弹回并看起来像 3)。
第一行现在正好是我想要的布局,它在configureCell:atIndexPath: 中计算。我猜这种行为的发生是因为单元被重新使用然后再次配置。
所以感觉好像我缺少某种初始化,我尝试调用pCell 和pCell.contentView 的setNeedsLayout 和layoutSubviews,但从未实现初始正确渲染。
只有当我将titleLabel 和dateLabel 的autoresizingMask 设置为UIViewAutoresizingNone 时,我才能得到正确的初始渲染,但是滑动删除不起作用,因为删除按钮在dateLabel 上渲染.
我必须在我的代码中进行哪些更改,以使所有单元格最初呈现就像第三张图片中的第一个单元格一样?
谢谢!
PS:我想内联图片,但不幸的是我没有足够的声誉。
【问题讨论】:
-
+1 用于使用
const float而不是#define以获得更好的类型安全性。我知道在 Apple 的示例代码中他们经常使用预编译器指令,但使用 consts 更好。恕我直言
标签: iphone cocoa-touch uikit uitableview