如何根據它們中的內容數量來擴展UITableViewCells?在我的單元格中,我使用了代表論壇的3個標籤。標籤被命名爲「別名」,「日期」和「評論」。第三個標籤評論,可以是任意數量的大小。因此,我的單元格需要根據「評論」標籤的大小動態變化。下面是我的相關代碼:定製UITableViewCell的高度
- (UITableViewCell *)tableView:(UITableView *)pTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ForumthreadCell";
UITableViewCell *cell = [pTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Feedback *item = [self.items objectAtIndex:indexPath.row];
UILabel *aliasLabel = (UILabel *)[cell viewWithTag:1];
UILabel *commentLabel = (UILabel *)[cell viewWithTag:2];
UILabel *dateLabel = (UILabel *)[cell viewWithTag:3];
[aliasLabel setText:item.alias];
[commentLabel setText:item.comment];
[dateLabel setText:[self.dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:(double)item.time]]];
commentLabel.numberOfLines = 0;
[commentLabel sizeToFit];
CGSize textHeight = [commentLabel.text sizeWithFont:commentLabel.font constrainedToSize:CGSizeMake(maxWidth, lineHeight * commentLabel.numberOfLines)lineBreakMode:UILineBreakModeWordWrap];
return cell;
}
正如你所看到的,我設定textHeight不同的commentsLabel的高度。但從現在開始我應該怎麼做?如果我知道標籤的高度,如何根據commentLabel的高度來設置單元的高度?請根據我的代碼給出代碼示例。我想我應該用下面的方法,但我不知道如何做到這一點:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
}
Thanx,這解決了我的問題!你終於解決了1天工作我,你是一個英雄! – user1293618
嗨,我發現了一個新的問題,自定義單元格,因爲我使用你的代碼,我想看看你是否知道這件事?這裏是鏈接:http://stackoverflow.com/questions/13223682/my-cells-resize-after-scrolling-upwards /關心 –