【问题标题】:Multiple cell types in section of UITableViewUITableView 部分中的多种单元格类型
【发布时间】:2013-06-24 12:23:45
【问题描述】:

我正在尝试汇总来自三个社交网络(Facebook、LinkedIn、Twitter)的数据。我有所有适当和正确的提要,我也有不同的细胞类型。

我想问的问题是,如何制作一个 UITableView,包含 10 个部分,每个部分按此顺序包含 3 个单元格(加上三种不同的单元格类型)

第 1 节:

[Feed 数组的 Facebook 单元格索引 0]

[Feed 数组的 Twitter 单元格索引 0]

[提要数组的LinkedIn单元格索引0]

第 2 节:

[Feed 数组的 Facebook 单元格索引 1]

[Feed 数组的 Twitter 单元格索引 1]

[供稿数组的LinkedIn单元格索引1]

第 3 节: 等等等等等等

【问题讨论】:

    标签: ios objective-c arrays uitableview


    【解决方案1】:

    使用表格视图的数据源和委托。重要的是对 3 种类型的单元格使用 3 种不同的单元格标识符(除非您希望它们具有相同的外观)。

    -numberOfSectionsInTableView: {
        return 10;
    }
    
    –tableView:numberOfRowsInSection: {
        return 3;
    }
    
    -tableView:cellForRowAtIndexPath:(NSIndexPath*)indexPath {
        if (indexPath.row == 0) {
            UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"FacebookCell"];
    
            if (cell == nil) {
                 // Init FB cell here
            }
    
            // Load FB feed data into the cell here
            return cell;
        }
        else if (indexPath.row == 1) {
            // Twitter Cell, remember to user a different cell identifier
        }
        else ...
    }
    

    【讨论】:

    • 就像我在下面得到的答案一样,这很好用,但问题是我需要在每个部分中增加单元格的值。我想说的是,在第一部分中,我希望所有单元格的所有提要中的索引为 0 的对象,在第 2 节中,所有单元格的所有提要中索引为 1 的对象等等..
    【解决方案2】:
     -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
     {
         return 3;
    
     }
    
     -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
       {
          return 10;
       }
    
     -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
     {
       static NSString * cellIdentifier = @"cellId";
        customCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
      if(indexPath.row == 0)
       {
            cell.textLabel.text = [FbFeed objectAtIndex:indexpath.section];
    
           // set FacebookCell
            cell
    
    
       }
      else if (indexPath.row == 1)
       {
        // set TwitterCell
        cell.textLabel.text = [tweetFeed objectAtIndex:indexpath.section];
    
       }
      else if (indexPath.row ==2)
      {
        cell.textLabel.text = [linkedinFeed objectAtIndex:indexpath.section];
    
    
        //set linkedin
      }
    
    return cell;
    }
    

    【讨论】:

    • 这很好用,但我应该如何增加我的提要数组?我的意思是,它为所有部分提供了相同的值,它不会增加提要数组中的对象。
    • 它始终在适当的提要数组中为我提供相同的索引。
    • 效果很好!谢谢你的帮助,希望有一天我能帮助你:)
    • @user2282749 我敢肯定,总有一天你会帮助我的! :)
    【解决方案3】:

    为了构建示例,这可用于任何多种类型的单元格。您不必总是使用行号来决定类型。您可以在行中获取一个对象并决定要显示的类型。

    另外,如果您使用情节提要,只需在表格中添加另一个原型单元格,为其分配一个唯一标识符,然后进行设置以便您可以使用它。如果您需要依赖于返回数据的不同布局,则效果非常好。

    -tableView:cellForRowAtIndexPath:(NSIndexPath*)indexPath {
        //Check what cell type it is. In this example, its using the row as the factor. You could easily get the object and decide from an object what type to use.
        if (indexPath.row == 0) {
            UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Type1Cell"];
    
            return cell;
        }
        else if (indexPath.row == 1) {
            UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Type2Cell"];
    
            return cell;
        }
        else {
            UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Type3Cell"];
    
            return cell;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多