2016-04-04 17 views
0

私のテーブルビューのセルで何か変わったことが起こっています。私は2つのカスタムセルを持っています。最初のセルは "HeaderCell"で、2番目のセルは "MapCell"です。奇妙なUITableView:numberOfRowsInSectionの動作ですか?

表には1つのセクションと2つの行があります(したがって、上記の2つのカスタムセル)。

cellForRowAtIndexPathメソッドでいくつかのログを追加しました。しかし、ログがコンソールに次のように出力さ

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell: UITableViewCell 

    print("outside if conditional (before) - section \(indexPath.section), row \(indexPath.row)") 

    if indexPath.row == 0 { 

     print("inside summarycell - section \(indexPath.section), row \(indexPath.row)") 

     let summaryCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! SummaryHeaderTableViewCell 

     // cell configuration goes here. 

     cell = summaryCell 
    } 
    else 
    { 
     let mapCell = tableView.dequeueReusableCellWithIdentifier("MapCell", forIndexPath: indexPath) as! MapTableViewCell 

     print("inside mapcell - section \(indexPath.section), row \(indexPath.row)") 

     // cell configuration goes here. 

     cell = mapCell 
    } 

    print("outside if (after) - section \(indexPath.section), row \(indexPath.row)") 

    return cell 
} 

::ここでは、全方法がある

つまり
outside if conditional (before) - section 0, row 0 
- inside summarycell - section 0, row 0 
outside if (after) - section 0, row 0 

outside if conditional (before) - section 0, row 1 
- inside mapcell - section 0, row 1 
outside if (after) - section 0, row 1 

outside if conditional (before) - section 0, row 0 
- inside summarycell - section 0, row 0 
outside if (after) - section 0, row 0 

outside if conditional (before) - section 0, row 1 
- inside mapcell - section 0, row 1 
outside if (after) - section 0, row 1 

、それは二回、すべてを印刷します。私のnumberOfSectionsInTableViewは1セクションを返し、numberOfRowsInSectionメソッドは2行を返します。

アプリは、それが必要として2つの行を示しシミュレータ、通常になります。

enter image description here

+3

2度呼ばれて何が間違っていますか?あなたは余分なreloadDataや何かを持っている可能性がありますが、それは問題を引き起こすものではありません。 – jtbandes

+2

これは正常です。テーブルビューは、そのデータソースに、セクションごとにいくつのセクションといくつの行がいくつあるかを尋ねることができます。 – rmaddy

+1

'UITableViewController'または' UITableView'を内部に持つ 'UIViewController'を使用していますか? – atulkhatri

答えて

1

それはあなたが照会するテーブルビューを引き起こしているreloadDataへの余分な呼び出しを持っている可能性がありますそのデータソースには余分な時間がかかります。

ただし、これによって問題は発生しません。データソースメソッドは、データに関するテーブルビューに情報を提供することを目的としています。同じ情報を2回入力すると、何も問題ありません。

+0

ありがとう..愚かな私! –