【发布时间】:2017-07-27 08:18:17
【问题描述】:
我的表格视图中有多个单元格;例如猫细胞、狗细胞、鸡细胞。在 Cat Cell 中,我有两个视图:图形视图和图像视图。
如果图形视图被隐藏而图像视图可见,那么我想将单元格高度设置为 200,在相反的情况下设置为 350。
我将如何实现这一目标?我正在表格视图的heightForRowAt 委托方法中注册我的 nib 文件,但我尝试了不同的方法无济于事。这是我单元格中的两个视图:
@property (weak, nonatomic) IBOutlet UIView *imagesView;
@property (weak, nonatomic) IBOutlet UIView *graphView;
...这是我的heightForRowAt 方法:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
TrainingImageCell *cell = (TrainingImageCell*)[self.tableView dequeueReusableCellWithIdentifier:@"TrainingImageCell"];
if (cell.isGraphViewOnTop == YES) {
return 350;
}
else {
return 200;
}
return 200;
}
【问题讨论】:
-
为什么要在 heightForRowAt 中注册 nib 文件。您正在注册新单元格,因此无法访问现有单元格。如果您希望它以您的风格工作,而不是在 IndexPath 处为行注册高度的新单元格,您可以使用 indexPath 作为 TrainingImageCell currentCell = (TrainingImageCell)[tableView cellForRowAtIndexPath:indexPath]; 访问现有单元格并检查属性。
-
在 heightForRowAtIndexPath 中使用 dequeueReusableCellWithIdentifier 方法是无效的。因为首先调用 heightForRowAtIndexPath 方法,然后调用 cellForRowAtIndexPath 方法。如果你给我看一些 cellForRowAtIndexPath 方法的代码,我可以帮助你吗?
-
我得到了那部分@YagneshDobariya 我必须像答案中提到的那样使用它,但我现在遇到了重新加载表格视图的问题。我已经定义了一个布尔属性 isShowingGraphView 并在注册笔尖后在 cellForRowAt 中隐藏和取消隐藏我的视图。我在 heightforRowAt 中调用此属性并反对 bool 返回高度,但如果我调用 reload TableView 整个视图就会消失。
-
@UsamabinAttique 你为什么使用 TrainingImageCell cell = (TrainingImageCell)[self.tableView dequeueReusableCellWithIdentifier:@"TrainingImageCell"];在 heightForRowAtIndexPath 中。直接使用与在 cellForRowAtIndexPath 中设置“isGraphViewOnTop”相同的条件。我认为问题应该在这里。
标签: ios objective-c uitableview uiview