【问题标题】:TableView with multiple prototype cells具有多个原型单元格的 TableView
【发布时间】:2013-01-23 09:41:03
【问题描述】:

我有一个关于具有 3 种不同原型单元格的表格视图的简单问题。前两个只出现一次,而第三个出现 4 次。现在我感到困惑的是如何在我的 cellforRowatindexpath 中指定哪个单元格原型用于哪一行。所以,我想要第 0 行,使用原型 1,第 1 行,使用原型 2,第 3、4、5 和 6 行使用原型 3。最好的方法是什么?我是否给每个原型一个标识符,然后使用 dequeueReusableCellWithIdentifier:CellIdentifier ? 能否提供一些示例代码?

编辑:

还是不行。这是我目前拥有的代码。 (我只有一个 switch 语句的情况,因为我只是想测试一下是否在第一行中生成了单元格,但当前表格视图为空白)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     switch(indexPath.row)
{          
 case 0: {static NSString *CellIdentifier = @"ACell";
                   UITableViewCell *cell = [tableView
                                           dequeueReusableCellWithIdentifier:@"ACell"];
  if(cell==nil) {
    cell=[[UITableViewCell alloc]
          initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"ACell"];

                        }
  return cell;
  break;
    }
  }
}

Acell 是我创建的单元原型的标识符。我

【问题讨论】:

  • 我不明白你的问题。
  • "我是否给每个原型一个标识符,然后使用 dequeueReusableCellWithIdentifier:CellIdentifier?"是的你是。您几乎已经回答了自己的问题。
  • 但是如何选择适用于哪一行的原型呢?
  • 喜欢 Best Coder 在他的回答中显示。
  • @rdelmar 在我的问题中查看编辑

标签: ios objective-c uitableview uistoryboard


【解决方案1】:

如果您使用三个原型,则使用三个标识符。因为只有一个标识符会导致问题。你会得到错误的结果。所以这样的代码。

if(indexPath.row==0){
 // Create first cell
}

if(indexPath.row==1){
 // Create second cell
}

else{
 // Create all others
}

您也可以在此处使用 switch case 以获得最佳性能。

【讨论】:

  • 在创建单元格期间,您可以遵循相同的方法,然后再使用单个标识符。但是在这里你将在双端队列中使用三个标识符。
【解决方案2】:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if (cell.tag == 0) 
   {
    return array1.count;
   }
   else(cell.tag == 1)
   {
    return array2.count;
   }    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 static NSString *cellIdentifier;

 NSString *membershipType = [membershipTypeArray objectAtIndex:indexPath.row];

 if ([membershipType isEqualToString:@"silver"]||[membershipType isEqualToString:@"gold"])
 {
     cellIdentifier = @"cell";
 }
 else if ([membershipType isEqualToString:@"platinum"])
 {
     cellIdentifier = @"premiumCustomCell";
     cell.customCellImageView.image = [cellImageArray objectAtIndex:indexPath.row];
 }

 cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

 if (!cell) {
     cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
 }
 cell.selectionStyle = UITableViewCellSelectionStyleNone;
 cell.headingLabel.text = [titleArray objectAtIndex:indexPath.row]; 
}

【讨论】:

  • 如何处理没有。部分中的行数。如果您有 2 个原型单元格并且没有基于数组计数显示的行数?
  • 您只需标记自定义原型单元格并检查每个单元格的数组计数以返回“array.count”
【解决方案3】:

我在这里写了如下代码:

#pragma mark == Tableview Datasource

  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return 2;
 }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger nRows = 0;
switch (section) {
    case 0:
        nRows = shipData.count;
        break;
    case 1:
        nRows = dataArray1.count;
        break;
    default:
        break;
}
return nRows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellIdentifier = @"cellIdentifier1";
NSString *cellIdentifier1 = @"cellIdentifier2";
SingleShippingDetailsCell *cell;
switch (indexPath.section) {
    case 0:
        cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        //Load data in this prototype cell
        break;
    case 1:
        cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
        //Load data in this prototype cell
        break;
    default:
        break;
}
return cell;
 }

【讨论】:

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