制作自定义的动态tableview单元格

时间:2014-01-23 10:29:43

标签: ios objective-c uitableview

我正在努力弄清楚如何完全控制我的tableview单元格。我希望他们看起来像这样:

App mockup

现在我需要知道如何正确管理我的细胞。我应该创建一个表查看单元格的子类吗?我应该在tableview的故事板中做这一切吗?这就是我现在正在做的事情。另外,如何根据文本行数实现动态单元格高度?

由于

3 个答案:

答案 0 :(得分:1)

您应该为UITableViewCell类创建子类,并使用XIB创建自己的自定义单元格。这将为你提供充足的动力空间。

请参阅本教程以了解如何执行此操作: http://www.appcoda.com/customize-table-view-cells-for-uitableview/

答案 1 :(得分:1)

您可以创建自定义视图,并在cellForRowAtIndex

中使用以下内容
static NSString * cellIdentifier=@"MyTableView";
UITableViewCell * cell;
if(cell== nil)
{
    cell = [myTableView dequeueReusableCellWithIdentifier:cellIdentifier];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    contentCell.tag =100;
     contentCell=[[ContentOfCell alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
    [cell.contentView addSubview:contentCell];

    }
    else{
        // Reusable part. Reuse the UI controls here from existing cell
        contentCell = (ContentOfCell *)[cell.contentView viewWithTag:100];
    }
    //Assign all the data here

    contentCell.nameField.text=[arr objectAtIndex:indexPath.section];
    //same way for other fields
   }

内容单元格是自定义视图

答案 2 :(得分:0)

我将尝试分三部分回答您的问题:

  1. 对于基于文本内容的动态单元格高度:您有一个名为heightForRowAtIndexPath的表视图委托,您应该根据其字体和字体大小特征计算文本的高度,当然,通过提供可用的宽度,您可以使用NSString的方法“sizeWithFont”。

  2. 要更好地控制单元格外观:您应该创建一个表格视图单元格子类并在cellForRowAtIndexPath中使用它。

  3. 你是否应该使用故事板:没有必要使用故事板。