1

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

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

第 1 节:

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

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

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

第 2 节:

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

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

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

第 3 部分:等等等等

4

3 回答 3

4

使用表格视图的数据源和委托。重要的是对 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 ...
}
于 2013-06-24T12:39:47.670 回答
3
 -(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;
}
于 2013-06-24T12:35:28.693 回答
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;
    }
}
于 2014-09-29T16:39:44.357 回答