奇怪的UITableView:numberOfRowsInSection行为?

时间:2016-04-04 14:33:40

标签: ios uitableview

我的tableview的细胞发生了一些奇怪的事情。我有两个自定义单元格,第一个是" HeaderCell",第二个是" MapCell"。

该表有一个部分和两行(因此上面描述了两个自定义单元格)。

我在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行。

应用程序在模拟器中看起来很正常,它显示了应该的两行:

enter image description here

1 个答案:

答案 0 :(得分:1)

您可能需要额外调用reloadData,这会导致表格视图在额外的时间内查询其数据源。

但是,这不应该导致任何问题。数据源方法旨在向表格视图提供有关数据的信息 - 如果您提供两次相同的信息,则没有任何问题。

相关问题