我有一个显示节目名称和DJ名称的UITableView
。对于某些节目,没有任何DJ。所以我通过添加2 UITableViewCell
创建了自定义UILabels
。 UITableViewCell
的中间显示程序名称,底部UILabel
显示DJ名称。要显示节目和DJ名称,我必须将cell size
和UITableViewCell
大小设置为147
。但是当DJ不可用时,我想隐藏底部标签,并将单元格和行高调整为70.所以我该怎么做?
我正在null
方法中检查此DJ是否为cellForRowAtIndexPath
。
我如何调整单元格和行高?
感谢您的帮助。
答案 0 :(得分:3)
您需要使用以下代理:
-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*) indexPath {
if(...){
return 147.0f; //or whatever
}
else{
return 70.f;
}
}
答案 1 :(得分:0)
在@implementation
下面添加NSIndexPathNSIndexPath *selectedCellIndexPath;
添加这两种方法。
// And in the implementation file:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
selectedCellIndexPath = indexPath;
// Forces the table view to call heightForRowAtIndexPath
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// Note: Some operations like calling [tableView cellForRowAtIndexPath:indexPath]
// will call heightForRow and thus create a stack overflow
if(selectedCellIndexPath != nil
&& [selectedCellIndexPath compare:indexPath] == NSOrderedSame)
return 338;
return 64;
}
答案 2 :(得分:0)
您有两种类型的细胞。高度为147和70像素。
1)正如Anoop Vaidya所说,你应该通过检查“DJ是否为空”来设置单元格高度。
2)为避免重复使用错误的单元格模板,您应该使用2种类型的cellIdentifier:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = nil;
if (check DJ is nil or not) {
CellIdentifier = @"CellWithHeight_147";
} else {
CellIdentifier = @"CellWithHeight_70";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
...
return cell;
}