kikily liu kikily liu - 3 months ago 19
Swift Question

ios swift uitableview add last cell

I want to add a cell into my tableview to the last row using another UserInterface.The code is like this.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:JoinCell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as! JoinCell

let lastSectionIndex = tableView.numberOfSections-1
let lastSectionLastRow = tableView.numberOfRowsInSection(lastSectionIndex) - 1
let lastIndexPath = NSIndexPath(forRow:lastSectionLastRow, inSection: lastSectionIndex)

let cellIndexPath = tableView.indexPathForCell(cell)

if cellIndexPath == lastIndexPath {
cell = tableView.dequeueReusableCellWithIdentifier("JoinFooterCell") as! JoinFooterCell
}


I got error message "Cannot assign value of type 'JoinFooterCell' to type 'JoinCell'"

Does anyone can give me advise? Thanks.

Answer

Do this and for datasource.sections.count use the same value you return in numberOfSectionInTableView() and for datasource.rows.count use the same value you return in your numberOfRowsForSection()

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if datasource.sections.count - 1 == indexPath.section && datasource.rows.count - 1 == indexPath.row {
        let cell = tableView.dequeueReusableCellWithIdentifier("JoinFooterCell") as! JoinFooterCell
        return cell
    } else {
        let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as! JoinCell
        return cell
    }
}