我試圖返回heightForRowAtIndexPath
委託方法中detailTextLabel
的高度。這樣我的表格視圖單元應該和我的detailTextLabel
一樣高。設置UITableViewCell的正確高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *text = cell.detailTextLabel.text;
CGSize size = cell.detailTextLabel.bounds.size;
UIFont *font = cell.detailTextLabel.font;
CGSize labelSize = [text sizeWithFont:font
constrainedToSize:size
lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height;
}
但是我在下面的行得到一個EXC_BAD_ACCESS
:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
WATS錯上面這段代碼?
'-tableView:heightForRowAtIndexPath:'在'-tableView:cellForRowAtIndexPath:'過程中被調用,所以當你調用'-cellForRowAtIndexPath:'時,單元還沒有被完全創建,所以你不能引用它。 –
[如何訪問tableView中的cell.textLabel.text:heightForRowAtIndexPath:from tableView:cellForRowAtIndexPath:](http://stackoverflow.com/questions/3903136/how-to-access-cell-textlabel-text-in -tableviewheightforrowatindexpath-from-tab)以及許多其他許多 – jrturton