Search code examples
iosobjective-cuitableviewcocoa-touchcustom-cell

Alternate two different custom cell layouts in a table view


In my storyboard I created two different prototype cells, cell1 and cell2. I want them to appear in my tableView like this:

TableView

prototype cell1 - indexpath.row[0]
prototype cell2 - indexpath.row[1]
prototype cell1 - indexpath.row[2]
prototype cell2 - indexpath.row[3]
and so on...

I experimented with if and switch statements and for loops in cellForRowAtIndexPath:, but couldn't get it right. Then I searched here and on the internet but still couldn't find an answer. Hopefully someone could explain it to me?


Solution

  • This is simple to do with the mod operator and a single if statement,

    if (indexPath.row %2 == 0) {
        //dequeue cell1 type
        // configure the cell
        // return cell
    
    }else{
        //dequeue cell2 type
        // configure the cell
        // return cell
    }