Iphone 自定义nib UITableViewCell高度

Iphone 自定义nib UITableViewCell高度,iphone,objective-c,uitableview,Iphone,Objective C,Uitableview,我在IB中创建了一个自定义UITableViewCell,将其链接到根视图控制器的属性,并在CellForRowatineXpath中设置它。但是我绘制的单元格的高度与我在IB中设置的不匹配,建议?下面是一些截图和代码。 addressCell是@property(非原子,赋值)IBUITableViewCell*addressCell;,并在IB中链接到文件的所有者(表视图控制器) 我正在使用Apple的table view编程指南中的示例。您可以使用以下方法调整单元格的高度: - (CG

我在IB中创建了一个自定义UITableViewCell,将其链接到根视图控制器的属性,并在CellForRowatineXpath中设置它。但是我绘制的单元格的高度与我在IB中设置的不匹配,建议?下面是一些截图和代码。

addressCell是@property(非原子,赋值)IBUITableViewCell*addressCell;,并在IB中链接到文件的所有者(表视图控制器)


我正在使用Apple的table view编程指南中的示例。

您可以使用以下方法调整单元格的高度:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    CGFloat result;
    result = 120.0f;
    return result;
}

这将适用于自定义单元格。

正如WrightsCS所说,这是一种方法。如果所有行的高度都相同,另一个选项是设置行本身。(前者的优点是允许您为每行返回任意值。)

我使用以下代码段。从nib文件中选择正确的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

   if (indexPath.row == 0) {
      return self.cell0.contentView.bounds.size.height;
   }
   if (indexPath.row == 1) {
      return self.cell1.contentView.bounds.size.height;
   }
   if (indexPath.row == 2) {
      return self.cell2.contentView.bounds.size.height;
   }

   return 44.0;
}
不要忘记在-(void)viewDidLoad中加载单元格

[[NSBundle mainBundle] loadNibNamed:@"YourView" owner:self options:nil];

IB中有两个地方需要设置行高。首先是单元格本身的自定义行高度。单击要调整大小的单元格,然后单击右侧“实用程序”窗口中的“大小检查器”(标尺)。在“表格视图”单元部分的顶部设置行高。单击“自定义”复选框。 然后单击左侧文档大纲窗口中的表视图。返回右侧“实用程序”窗口中的“大小检查器”,将整个表格的行高设置为所需的高度。

使用此实现,您无需向tableviewcontroller添加任何代码。

创建表格视图时,我会这样做,以确保行高与单元格nib中定义的行高匹配:

- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]
                      initWithFrame:self.view.bounds
                      style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;

        [_tableView
         registerNib:[UINib nibWithNibName:@"<CELL NIB NAME>" bundle:nil]
         forCellReuseIdentifier:kCellIdentifier];

        // Get the cell's root view and set the table's 
        // rowHeight to the root cell's height.
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"<CELL NIB NAME>"
                                                     owner:self
                                                   options:nil];
        UIView *cellView = (UIView *)nib[0];
        if (cellView) {
            _tableView.rowHeight = cellView.bounds.size.height;
        }        
    }
    return _tableView;
}
-(UITableView*)表视图
{
如果(_tableView==nil){
_tableView=[[UITableView alloc]
initWithFrame:self.view.bounds
样式:UITableViewStylePlain];
_tableView.dataSource=self;
_tableView.delegate=self;
[\u tableView
registerNib:[UINib nibWithNibName:@”“bundle:nil]
强制标识符:kCellIdentifier];
//获取单元格的根视图并设置表的
//行高到根单元格的高度。
NSArray*nib=[[NSBundle mainBundle]loadNibNamed:@“”
所有者:self
选项:无];
UIView*cellView=(UIView*)nib[0];
如果(cellView){
_tableView.rowHeight=cellView.bounds.size.height;
}        
}
返回_tableView;
}

我希望这能有所帮助。

对于那些仍然对替代答案感兴趣,并且不想在索引路径中实现高度的人,好消息:)

您可以按如下方式设置de xib中的行高:

这还不够。事实上,这在运行时将被完全忽略。解决方案是通过设置单元格内容的顶部和底部约束来设置中心大小。内容将推动其容器的顶部和底部,这将导致更高的单元格:)


太糟糕了,我知道RowAtIndexPath的高度,但我希望使用基于NIB的单元格可以避免使用它。谢谢我有同样的问题,同样的问题。如果我们在IB上设置单元格的高度,为什么还要将其设置为HeightForRowatineXpath?Apple使用nib中具有不同高度的自定义单元格的示例也使用了由控制器加载的基于nib的表视图。我猜测(尚未测试)使用[tableView dequeueReusableCellWithIdentifier:]会导致使用默认高度而不是nib的设置。获取单元格本身的高度而不是硬代码应该更可取。问题是如何做到这一点?我将我的表从原型单元格重构为NIB,即使使用dequeueReusableCellWithIdentifier,它也能在原型单元格上工作。我已经设置了笔尖单元格的高度,这些单元格现在有44个点高实际上,如果你定义了要更改高度的单元格,它们的高度就不一样了,所以你的错误就在于此。在我的例子中你是对的,嗯?这就是我的观点:委托方法允许行不同。它们不在你的例子中,但它们可能是。设置
rowHeight
属性不会为您提供该选项。请点击此链接并获取问题的答案-[get row height from NIB file][1][1]:因此,我在NIB文件中设置了行高,并为RowAtIndexPath设置了高度。然后我花了1个小时的时间来思考UI为什么会变得疯狂。我还需要在tableView属性中设置高度。Xcode太糟糕了。无论如何,谢谢你的回答!解决问题的优雅和最佳方法imho
- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]
                      initWithFrame:self.view.bounds
                      style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;

        [_tableView
         registerNib:[UINib nibWithNibName:@"<CELL NIB NAME>" bundle:nil]
         forCellReuseIdentifier:kCellIdentifier];

        // Get the cell's root view and set the table's 
        // rowHeight to the root cell's height.
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"<CELL NIB NAME>"
                                                     owner:self
                                                   options:nil];
        UIView *cellView = (UIView *)nib[0];
        if (cellView) {
            _tableView.rowHeight = cellView.bounds.size.height;
        }        
    }
    return _tableView;
}