2011-04-07 57 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 
}