2016-08-19 12 views
1

別のUserInterfaceを使用して、最後の行にテーブルビューにセルを追加したいと思います。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

答えて

1

これを行うと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

ありがとうございました。できます! –

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を得ることができません。 –

0

に戻ると同じ値を使用します。

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を得ることができません。なぜなら、セルは何もないからです。 –

関連する問題