ios swift uitableview添加最后​​一个单元格

问题描述:

我想添加一个单元格到我的tableview到最后一行使用另一个UserInterface.The代码是这样的。ios swift uitableview添加最后​​一个单元格

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 
    } 

我收到错误消息“无法分配型“JoinFooterCell为键入‘JoinCell’的价值”

有谁可以给​​我建议?谢谢。

+0

欢迎堆栈溢出!你可以帮助我们通过格式化来帮助你,所以我们没有滚动它。 – zhon

做到这一点,对于datasource.sections.count使用相同的值,你在numberOfSectionInTableView()返回和datasource.rows.count使用相同的值,你在你的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 
    } 
} 
+0

非常感谢。有用! –

做这样的事情

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 
} 

究竟是什么我试图做的是创建要返回小区。不要初始化单元格,然后尝试重新分配。首先确定它是否是最后一个单元,并根据它初始化你想要的单元类型。

+0

非常感谢您的快速回复。声明单元格后,我无法获取indexPathForCell。 –

返回让试试这个:

var cell: UITableViewCell! 
if cellIndexPath == lastIndexPath { 
    cell = tableView.dequeueReusableCellWithIdentifier("JoinFooterCell") as! JoinFooterCell 
} else { 
    cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as! JoinCell 
} 
return cell 
+0

非常感谢您的快速回复。我无法获取indexPathForCell,因为单元格是没有的。 –