【发布时间】:2011-08-21 01:43:10
【问题描述】:
我有一组 UILabel,它们显示在 TableView 的单元格中。当用户触摸单元格时,标签会展开以显示我放入其中的所有文本,而不是截断的两行版本。
动画和扩展有效,但是当我再次按下标签以使其缩小 TableViewCell 时,标签正确调整大小,但标签没有。我已经对标签的大小进行了 NSLog,并且以编程方式我的代码可以正常工作,但无法正确绘制。
这是我的 cellForRowAtIndexPath 方法:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *sectionContents = [[self tableData] objectAtIndex:[indexPath section]];
NSString *contentForRow = [sectionContents objectAtIndex:[indexPath row]];
UILabel *label = nil;
int noOfLines;
UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"];
label = [[UILabel alloc] initWithFrame:CGRectZero];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
label.text = contentForRow;
if ([expandedIndexPathsArray containsObject:indexPath])
{
noOfLines = 0;
CGSize size = [contentForRow sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
label.frame = CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f));
NSLog(@"height is now %f",label.frame.size.height);
} else {
noOfLines = 2;
CGSize size = [contentForRow sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
label.frame = CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MIN(size.height, 44.0f));
NSLog(@"height is now %f",label.frame.size.height);
}
NSLog(@"Number of Lines: %d",noOfLines);
label.lineBreakMode = UILineBreakModeWordWrap;
label.backgroundColor = [UIColor blueColor];
label.font = [UIFont systemFontOfSize:FONT_SIZE];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
label.numberOfLines = noOfLines;
[[cell contentView] addSubview:label];
[label release];
return cell;
}
谁能告诉我这里发生了什么,因为我很难理解它。
提前致谢! :D
【问题讨论】:
-
好的,我发现我的 cellForRowAtIndexPath 实际上是在旧标签上叠加新标签,而不是先删除它们。我是否需要以某种方式编辑已经存在的标签,还是删除它的子视图并添加一个新的?这会发生在 cellForRowAtIndexPath 还是 didSelectRowAtIndexPath 中?
标签: iphone objective-c ios uitableview uilabel