Reputation: 37
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.
Upvotes: 1
Views: 2121
Reputation: 9256
Let try this:
var cell: UITableViewCell!
if cellIndexPath == lastIndexPath {
cell = tableView.dequeueReusableCellWithIdentifier("JoinFooterCell") as! JoinFooterCell
} else {
cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as! JoinCell
}
return cell
Upvotes: 0
Reputation: 3074
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
}
}
Upvotes: 1
Reputation: 2908
Do something like this
let lastSectionIndex = tableView.numberOfSections-1
let lastSectionLastRow = tableView.numberOfRowsInSection(lastSectionIndex) - 1
let lastIndexPath = NSIndexPath(forRow:lastSectionLastRow, inSection: lastSectionIndex)
let cellIndexPath = tableView.indexPathForCell(cell)
var cell
if cellIndexPath == lastIndexPath {
cell = tableView.dequeueReusableCellWithIdentifier("JoinFooterCell") as! JoinFooterCell
}
else {
cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as! JoinCell
}
What actually am trying to do is create cell which you want to return. Don't initialize the cell first and then try to re assign. First determine whether its the last cell and based on that, initialize the kind of cell you want.
Upvotes: 0