luke
luke

Reputation: 2773

Creating multiple sections with custom cells

So I have 2 custom cells inside a table view. Each cell has it's own individual section, "Filter" and "Sort By"

func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell {
    if indexPath.row == 0 {
        let cell = tableView.dequeueReusableCellWithIdentifier("FilterCell") as! FilterCell
        // Edit cell here
        return cell
    } else {
        let cell = tableView.dequeueReusableCellWithIdentifier("SortByCell") as! SortByCell
        // Edit cell here
        return cell
    }
}
func tableView(tableView:UITableView, numberOfRowsInSection section:Int) -> Int {
return 1
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 2 }
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    switch section {
    case 0:
        return "Sort By"

    default:
        return "Filter"
    }
}

I'm trying to display each section with these headers but the problem is that both sections display the same custom cell. My assumption is that I need each section to detect what custom cell to display through the identifier but I can't seem to figure this out. I'm not sure if i should be using a switch statement neither but does seem plausible

Upvotes: 1

Views: 696

Answers (1)

Ro22e0
Ro22e0

Reputation: 394

Please dude...

func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell {
    if indexPath.section == 0 {
        let cell = tableView.dequeueReusableCellWithIdentifier("SortByCell") as! SortByCell
        // Edit cell here
        return cell
    }
    let cell = tableView.dequeueReusableCellWithIdentifier("FilterCell") as! FilterCell
        // Edit cell here
    return cell
}

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

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

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    switch section {
    case 0:
        return "Sort By"
    default:
        return "Filter"
    }
}

Upvotes: 1

Related Questions