Adding multiple user cells to a UITableView

Although this is one of the most frequently asked questions, but I could not find a comprehensive answer. I need to have custom cells in a UITableView. Some of them contain labels or text fields, and some contain images and buttons. I made separate classes for each type of cell. I am using a GroupStyle table with several partitions. Right now I am adding cells to cellForIndexPath with the key for the section and if-else for the rows in the section:

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; 

I have to return the cell at the end, because due to the definition of the cells inside the blocks of code, the cell becomes unrecognizable. To solve this problem, I declared a cell with id on top. But I know this is the wrong way. How can I enable this ad and access multiple types of cells?

At the moment, there are 4-5 lines that correspond to one screen and do not need to scroll. So, I do not use cells. But when editing, more lines will be compressed. And in another table there are more rows that can scroll across the screen. This means that I have to reuse the cells. So, the second part of my question is: how can I reuse multiple user cells?

+7
source share
2 answers

To answer your first question, you can return nil since you do not have a good return value. If he ever hits this occasion, an exception will be thrown; as it is now, it will most likely give you EXC_BAD_ACCESS somewhere inside the frame code.

To answer your second question, each cell type must have a unique reuse identifier. For example, an entire CellA may have a reuse identifier @ "CellA". Then you will use them in the same way as if all the cells were the same: when you need a call to CellA [tableView dequeueReusableCellWithIdentifier:@"CellA"] , when you need a call to CellB [tableView dequeueReusableCellWithIdentifier:@"CellB"] and t .d. For example,

  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; 
+11
source

Add UITableView to UIView.Add custom cells, bind custom cell classes and implement the delegates -UITableviewDelegate and UITableViewDataSource.

case 1: two custom cells in table view

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 } 

case 2: alternative display of user cells (i.e. using uisegment control)

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

case 3: Alternative custom cells (i.e. odd evens)

 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 } 
0
source

All Articles