Objective c 自定义UITableViewCell未完整显示

Objective c 自定义UITableViewCell未完整显示,objective-c,xib,Objective C,Xib,我是iOS新手。我正在练习objective-c,并制作了我的自定义UITableViewCell。它有一些高度,而不是它的默认高度,即高度为120。但当我运行程序时,单元格会显示在其默认高度上。我只编写了以下代码,我正在使用xib设计定制单元。如果需要,我可以分享截图。谢谢 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { s

我是iOS新手。我正在练习objective-c,并制作了我的自定义UITableViewCell。它有一些高度,而不是它的默认高度,即高度为120。但当我运行程序时,单元格会显示在其默认高度上。我只编写了以下代码,我正在使用xib设计定制单元。如果需要,我可以分享截图。谢谢

- (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->Size Inspector->行高

2) 在viewDidLoad()中,可以定义高度
\u tableView.rowHeight=120此选项覆盖序列图像板/界面生成器中的值

3) 您可以按照其他人的建议实现委托

(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 120;
}
最后一个选项使您可以灵活地为每行定义不同的高度,并替代上述两个选项中的值

有多种方法可以根据内容获取动态单元格高度。如果你需要帮忙,请告诉我