使用多个Prototype Cell时UITableView的奇怪行为

时间:2015-02-02 23:19:40

标签: ios uitableview swift prototype cells

所以我有一个UITableView,我计划在其中使用2个原型单元。我们称它们为Cell A和Cell B. Cell A有自己的布局,Cell B有自己的布局。

这就是典型的UITableView实现中只有1个原型单元,在设置了所有单元格及其属性后,cellForRowAtIndexPath负责根据numberOfRowsInSection中的(x)项数填充所有行。

这是我遇到问题的地方。我已经在我的UITableView中实现了两个原型单元格,当我运行它时,我注意到cellForRowAtIndexPath只被调用了两次,即使我有一个(x)项中的值大于2的值。无所谓我把它设置为,它只被调用两次。我已经有了必要的if语句来根据单元格索引等选择单元格原型......所以这不是问题。问题是cellForRowAtIndexPath只被调用两次而不是循环遍历所有项目。

为什么会这样,我该如何解决?

这是我的DataSource方法代码:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 8

}

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

    Scripts.log("Data Count = \(indexPath.row)")

    if indexPath.row == 0{

        var cell: ContactDetailImageCell = tableView.dequeueReusableCellWithIdentifier(NAME_OF_CUSTOM_IMAGE_CELL) as ContactDetailImageCell

        cell.cardPhoto.image = aContact.profilePicture
        cell.fullName.text = aContact.getDisplayName()
        cell.workplace.text = aContact.workplace

        return cell

    }
    else{

        var cell: ContactDetailPhoneNumCell = tableView.dequeueReusableCellWithIdentifier(NAME_OF_CUSTOM_PHONE_CELL) as ContactDetailPhoneNumCell

        return cell

    }

    return UITableViewCell()

}

1 个答案:

答案 0 :(得分:2)

你的细胞高度是多少?只有在表认为需要显示单元格时才会调用cellForRowAtIndexPath。因此整个重用机制。因此,如果您有8个单元格并且它认为2填满了屏幕,那么在向上/向下滚动之前它将不会再要求。

你为heightForRowAtIndexPath返回了什么。