uitableview - 在不同部分( Storyboard)设置不同的行高

标签 uitableview height cgfloat

我有一个表格 View ,其中包含我在 Storyboard中设计的多个原型(prototype)单元格,但我遇到了高度问题,因为我的第一个单元格应该与第二个单元格不同,依此类推...我有不同的标识符对于每个单元格,因为我在 Storyboard中设计了它们,所以我知道它们的高度。我的代码中有这个,但它不起作用,有人知道如何修复它吗?:

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

UITableViewCell *cell = [[UITableViewCell alloc]init];
switch (indexPath.section) 

{
    case 1:

        cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        return 743.0f; 

        break;

    case 2:

        cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        return 300.0f;



}

}

感谢您的宝贵时间。

最佳答案

看起来您正在尝试将此方法用于其设计目的之外的目的... 您需要重写该方法:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section)
        case 1:
           static NSString *CellIdentifier = @"cell1";
           break;
        case 2:
           static NSString *CellIdentifier = @"cell2";
           break;

    UITableViewCell *cell = [tableView 
      dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    return cell;
}

仅更改 heightForRowAtIndexPath 中的行高:

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

switch (indexPath.section) 

{
    case 1:

        return 743.0f; 

        break; //technically never used

    case 2:

        return 300.0f;



}

查看本教程 http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1这是一个很好的资源

关于uitableview - 在不同部分( Storyboard)设置不同的行高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11488805/

相关文章:

ios - UITableView insertRows(位于: indexPath) returns error of invalid number of rows

Swift image Aspect Fill 高图像不起作用

html - 100% 宽度的页眉和页脚 bur 具有 100% 高度的固定内容

ios - 如何在 Swift 中获取两个 CGFloats 的最小值?

Swift 在 0 和 1 之间随机 float

ios - 在 Swift 中将 CGFloat 转换为 NSNumber

ios - 具有 Image SubView 性能问题的 TableView

ios - 自定义单元格不使用 Storyboard在 UITabelView 中显示数据

ios - 收到通知时刷新 TableView

height - 更改字体大小后更改 sublime text 默认主题中的行高