我有一组显示在TableView单元格中的UILabel。当用户触摸单元格时,标签将展开以显示我放入其中的所有文本,而不是两行截断版本。iOS UILabel不会重绘为新尺寸
动画和展开工作,但是当我再次按下标签使其缩小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? – Chris