2011-04-07 104 views
7

尽管这是最常问的问题之一,但我找不到一个全面的答案。我需要在UITableView中定制单元格。一些包含标签或文本字段,一些包含图像和按钮。我已经为每种类型的单元制作了单独的类。我正在使用具有多个部分的GroupStyle表。现在我将与开关的情况下cellForIndexPath细胞部分,如果其他的行节:在UITableView中添加多个自定义单元格

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的cell。但是我知道这不是正确的方法。我该如何解决多种类型单元格的声明和访问问题?

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

+0

你并不真正需要的switch-case – Markinson 2015-07-02 23:27:48

回答

11

要回答你的第一个问题,你不妨返回nil,因为你没有什么好的回报。如果它遇到这种情况,将抛出异常;就像现在这样,它可能会在框架代码的某个地方给你一个EXC_BAD_ACCESS。

要回答你的第二个问题,每个类型的应该有一个唯一的重用标识符。例如,所有的CellA都可以拥有@“CellA”的重用标识符。然后,您将完全按照所有单元格相同的情况重新使用它们:当您需要CellA呼叫[tableView dequeueReusableCellWithIdentifier:@"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; 
+0

谢谢你的答复。为了安全起见,我定义了一个默认情况下的单元格,并希望任何剩余的情况都可以默认处理,并且不会触及最后一个return语句。 – WaJiyaz 2011-04-08 21:01:13

0

添加的UITableView要UIView.Add定制单元,联想自定义单元格类和落实代表-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 
} 
相关问题