CHM
CHM

Reputation: 621

Multiple Custom Cells in TableViewController

Fairly new at this. I'm trying to populate 3 custom cells into a TableViewController.

I've managed (with help) to get 2 to load with no trouble. This is the code i use for 2:

override func viewDidLoad() {
super.viewDidLoad()

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44

}


override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 2
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    if indexPath.row % 2 == 0 {
        let cell = tableView.dequeueReusableCellWithIdentifier("FirstCell") as! FirstTableViewCell
        cell.artImageView.image = UIImage(named: "art")

        return cell

    } else {
        let cell = tableView.dequeueReusableCellWithIdentifier("SecondCell") as! SecondTableViewCell
        cell.kidsImageView.image = UIImage(named: "kids")


        return cell
    }

When I try to add a third row, this is where I run into trouble. This is the code i'm using:

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 44

}


override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 3
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    if indexPath.row % 3 == 0 {
        let cell = tableView.dequeueReusableCellWithIdentifier("FirstCell") as! FirstTableViewCell
        cell.artImageView.image = UIImage(named: "art")

        return cell

    } else if {
        let cell = tableView.dequeueReusableCellWithIdentifier("SecondCell") as! SecondTableViewCell
        cell.kidsImageView.image = UIImage(named: "kids")


        return cell

    } else {
        let cell = tableView.dequeueReusableCellWithIdentifier("ThirdCell") as! ThirdTableViewCell
        cell.pastaImageView.image = UIImage(named: "pasta")


        return cell
    }

}

Any help would be great. Gracias.

Upvotes: 0

Views: 986

Answers (1)

Walter
Walter

Reputation: 5887

Your issue starts with this line

  if indexPath.row % 3 == 0 {

If you really want the cells to be laid out 1,2,3 you can just do this:

 if indexPath.row  == 0 {
    let cell = tableView.dequeueReusableCellWithIdentifier("FirstCell") as! FirstTableViewCell
    cell.artImageView.image = UIImage(named: "art")

    return cell

} else if indexPath.row == 1 {
    let cell = tableView.dequeueReusableCellWithIdentifier("SecondCell") as! SecondTableViewCell
    cell.kidsImageView.image = UIImage(named: "kids")


    return cell

} else if indexPath.row == 2 {
    let cell = tableView.dequeueReusableCellWithIdentifier("ThirdCell") as! ThirdTableViewCell
    cell.pastaImageView.image = UIImage(named: "pasta")


    return cell
}

using indexPath.row % 3 == 0 is doing modulus math so it is giving you the "FirstCell" only when indexPath.row/3 is equal to an integer. Then you have an else if that has no test so it is getting called for all other rows. Finally your else never gets called.

Upvotes: 2

Related Questions