自定义UITableViewCell不全显示
问题描述:
我是iOS新手。我练习Objective-C并制作了自定义的UITableViewCell。它有一定的高度,而不是它的默认高度,即高度是120.但是当我运行该程序时,单元格显示为默认高度。我只写了一些代码,如下所示,我使用xib来设计自定义单元格。如果需要,我可以分享截图。由于自定义UITableViewCell不全显示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
return cell;
}
编辑在我的项目我还没有实现heightforrowatindexpath方法之一。为什么我必须在另一个项目中实施。是必要的?请指导。
答
答
行的高度必须在委托方法返回:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 120.0;
}
答
如果所有的自定义单元格是高度相同但与默认高度不同,您有三个选项可以定义高度。
1)将其定义为故事板/界面生成器中的TableView的属性。选择TableView - >尺寸检查器 - >行高。
2)在您的viewDidLoad()中,您可以定义高度_tableView.rowHeight = 120;
此选项将覆盖故事板/界面构建器中的值。
3)您可以像其他人一样实施代理。
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 120;
}
这最后一个选项,您可以灵活地定义不同高度的每一行,并从上述两个选项覆盖值。
有一些方法可以根据内容获取动态单元高度。如果您需要帮助,请告诉我。