繁体   English   中英

UITableView部分中的多种单元格类型

[英]Multiple cell types in section of UITableView

我正在尝试从三个社交网络(Facebook,LinkedIn,Twitter)聚合数据。 我拥有所有适当且正确的提要,并且它们的单元格类型也有所不同。

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

第1节:

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

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

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

第2节:

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

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

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

第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;
}

播放表视图的数据源和委托。 重要的是对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 ...
}

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

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

-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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM