我有一张自定义单元格的表格。这些细胞有不同的高度,我知道如何计算高度。问题是 heightForRowAtIndexPath 方法在方法 cellForRowAtIndexPath 之前被调用,所以我无法传递返回的CGFloat。
有没有办法在之后调用此方法?
非常感谢
修改
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CustomCellFavorites *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCellFavorites alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.topSuperiore = self;
}
//textviews
NSString *string01 = ((Traduzioni *)[self.arrayTraduzioni objectAtIndex:indexPath.row]).testoOriginale;
NSString *string02 = ((Traduzioni *)[self.arrayTraduzioni objectAtIndex:indexPath.row]).testoTradotto;
[cell loadItem:string01 :string02];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCellFavorites *cell = (CustomCellFavorites*)[self tableView:tableView cellForRowAtIndexPath:indexPath];
return cell.heightCell;
}
自定义单元格方法
-(void)loadItem:(NSString *)text01:(NSString *)text02 {
textView01.text = text01;
CGRect rect = textView01.frame;
rect.size.height = textView01.contentSize.height;
textView01.frame = rect;
[tv01Bg setFrame:CGRectMake(32, 0, 288, (textView01.contentSize.height)+20)];
textView02.text = text02;
CGRect rect2 = textView02.frame;
rect2.size.height = textView02.contentSize.height;
textView02.frame = rect2;
[tv02Bg setFrame:CGRectMake(0, (textView01.contentSize.height)+20, 320, (textView02.contentSize.height)+20)];
heightCell = (rect2.origin.y)+(rect2.size.height)+15.5;
}
答案 0 :(得分:3)
不,之后无法调用此方法。我处理这个问题的方法是基本上得到实际的行和根据行的类型,决定高度。一些简短的代码如下 -
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSObject *res = [self.resultSet objectAtIndex:[indexPath row]];
if([res isKindOfClass:[someX class]])
return 110;
else if([res isKindOfClass:[someY class]])
return 120;
else if([res isKindOfClass:[someZ class]])
return 230;
else
return 175.0;
}