8

虽然这是被问到最多的问题之一,但我找不到一个全面的答案。我需要在 UITableView 中有自定义单元格。有些包含标签或文本字段,有些包含图像和按钮。我为每种类型的细胞制作了单独的类。我正在使用具有多个部分的 GroupStyle 表。现在我在 cellForIndexPath 中添加单元格,其中部分的 switch-case 和部分中的行的 if-else:

id cell;
switch(indexPath.section) {
    case 0:
           if(indexPath.row==0) {
               CellA *cell = [[[CellA alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"Celld%",indexPath.row]] autorelease];
               //configure cell
               return cell;
           }
           else if(indexPath.row==1) {
               CellB *cell = [[[CellB alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"Celld%",indexPath.row]] autorelease];
               //configure cell
               return cell;
           }
           break;
    case 1:
           if(indexPath.row==0) {
               CellC *cell = [[[CellC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"Celld%",indexPath.row]] autorelease];
               //configure cell
               return cell;
           }
           break;
    default:
            break;
}
return cell;

我也必须在最后返回单元格,因为由于代码块内单元格的定义,单元格变得无法识别。为了解决它,我在顶部声明了带有 id 的单元格。但我知道这不是正确的方法。如何解决多种单元格的声明和访问问题?

目前有 4-5 行适合一个屏幕,不需要滚动。所以,我没有重复使用细胞。但是在编辑时会挤进更多的行。而在另一个表中,有更多的行可以滚动屏幕。这意味着我必须重复使用单元格。所以,我的问题的第二部分是;如何重用多个自定义单元格?

4

2 回答 2

11

要回答您的第一个问题,您还不如退货nil,因为您没有什么好的回报。如果遇到这种情况,将抛出异常;就像现在一样,它可能会在框架代码中的某处为您提供 EXC_BAD_ACCESS。

要回答您的第二个问题,每种类型的单元格都应该有一个唯一的重用标识符。例如,所有的 CellA 都可以有一个重用标识符 @"CellA"。然后,您将像在所有单元格都相同的情况下一样重用它们:当您需要[tableView dequeueReusableCellWithIdentifier:@"CellA"]CellA 调用时,当您需要 CellB 调用[tableView dequeueReusableCellWithIdentifier:@"CellB"]时,等等。例如,

    case 0:
        if (indexPath.row == 0) {
            CellA *cell = [tableView dequeueReusableCellWithIdentifier:@"CellA"];
            if (!cell) {
                cell = [[[CellA alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellA"] autorelease];
            }
            // configure cell
            return cell;
        }
        else if (indexPath.row == 1) {
            CellB *cell = [tableView dequeueReusableCellWithIdentifier:@"CellB"];
            if (!cell) {
                cell = [[[CellB alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellB"] autorelease];
            }
            // configure cell
            return cell;
        }
        break;
    case 1:
        if (indexPath.row == 0) {
            CellC *cell = [tableView dequeueReusableCellWithIdentifier:@"CellC"];
            if (!cell) {
                cell = [[[CellC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellC"] autorelease];
            }
            // configure cell
            return cell;
        }
        break;
于 2011-04-07T02:11:16.377 回答
0

将 UITableView 添加到 UIView。添加自定义单元格,关联自定义单元格类并实现委托 -UITableviewDelegate 和 UITableViewDataSource 。

案例 1:tableview 上的两个自定义单元格

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

  var cell: CustomCell!

  if indexPath.row == 0{
    cell = tableView.dequeueReusableCellWithIdentifier("Cell1ID", forIndexPath: indexPath) as CustomCell
    //set cell2
  }
  if indexPath.row >= 1{
    cell = tableView.dequeueReusableCellWithIdentifier("Cell2ID", forIndexPath: indexPath) as CustomCell
    let cons = aArray[indexPath.row - 1]
    // set cell2 
  }
  return cell
}

案例2:自定义单元格的交替显示(即使用uisegmentcontrol)

var CellIdentifier: String = "Cell1ID"

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
       if (CellIdentifier == "Cell1ID")
    {
let cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier)! as! FirstTableViewCell

//additional code
return cell

}
else {
let cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier)! as! SecondTableViewReportsCell

//Additional code

return cell
}
}

案例 3:备用自定义单元格(即奇偶数)

var CellIdentifier: String = "Cell1ID"

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

var cell: CustomCell!

if (indexPath.row % 2 == 0) {

let cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier)! as! FirstTableViewCell
   
}
else
{
        let cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier)! as! SecondTableViewCell
}
return cell
}
于 2016-02-11T07:10:55.773 回答