【问题标题】:Swift : How to have 2 different cells in 1 tableView [closed]Swift:如何在 1 个 tableView 中有 2 个不同的单元格 [关闭]
【发布时间】:2015-08-15 18:45:26
【问题描述】:

我想在我的表格视图中有 2 个不同的单元格。

例如:

  1. 我的第一个单元格有 1 个图像视图和 1 个标签。
  2. 我的第二个单元格有 4 个图像视图和 4 个标签

有可能吗?我怎样才能做到这一点?

谢谢。

【问题讨论】:

  • 您是否使用带有原型单元的情节提要?
  • 我有原型单元,我只是想使用静态单元。我只有 2 个单元格,所以没有动态表格视图。

标签: ios xcode swift


【解决方案1】:

创建两个自定义 UITableViewCell 并将它们注册到 tableView 与 cellIdentifiers 像 "CellType1" 和 cellType2 然后你可以在 cellForRowAtIndexPath 中做这样的事情

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = nil;
if(indexPath.row == 0)
{
// first row, return cell you want 
cellIdentifier = @"CellType1";
}
else 
{
// other rows you can return cell you want
cellIdentifier = @"CellType2";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
return cell;
}

【讨论】:

    【解决方案2】:

    故事板版本

    1. 只需拖放一个 TableViewController ,然后在右侧面板上设置属性。将类型设置为原型,您可以将任何您想要的东西放到它上面,例如 imgView 或 UILabel
    2. 然后在控制器中,只需使用您刚刚在
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 中创建的单元格

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多