English 中文(简体)
Adding multiple custom cells in UITableView
原标题:

Though this is one of the most asked question but i could not find one comprehensive answer. I need to have custom cells in UITableView. Some containing labels or text fields and some with images and buttons. I have made separate classes for each type of cell. I am using GroupStyle table with multiple sections. Right now I am adding cells in cellForIndexPath with switch-case for section and if-else for rows in 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 cell at the end as well because due to definition of cells inside code blocks, cell becomes unrecognizable. To solve it, i declared cell with id on top.But i know this is not the right way. How can I resolve this declare and access issue of multiple types of cells?

There are 4-5 rows at the moment which fit one screen and do not need scrolling. So, I am not reusing cells. But more rows will squeeze in while editing. And in another table, there are more rows which can scroll of the screen. This means I must reuse cells. So, second part of my question is; how can I reuse multiple custom cells?

最佳回答

To answer your first question, you may as well return nil as you have no good value to return. If it ever hits this case, an exception will be thrown; as it is now, it s likely to give you an EXC_BAD_ACCESS somewhere inside the framework code.

To answer your second question, each type of cell should have a unique reuseIdentifier. For example, all the CellA s could have a reuseIdentifier of @"CellA". Then you would reuse them exactly as you would in the case that all the cells were the same: when you need a CellA call [tableView dequeueReusableCellWithIdentifier:@"CellA"], when you need a CellB call [tableView dequeueReusableCellWithIdentifier:@"CellB"], and so on. 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;
问题回答

Add UITableView To UIView.Add custom cells, associate custom cell classes and implement delegates -UITableviewDelegate and UITableViewDataSource .

case 1: Two custom cells on 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
}

case 2: alternate display of custom cells (i.e. using 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
}
}

case 3: Alternate custom cells (i.e., odd even)

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
}




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签