ios - 使用超过 1 个 Prototype Cell 时 UITableView 的奇怪行为

标签 ios uitableview swift prototype cells

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

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

这是我遇到问题的地方。我已经在我的 UITableView 中实现了我的原型(prototype)单元格,当我运行它时,我注意到 cellForRowAtIndexPath 只被调用了两次,即使我在 (x) 个项目中有一个值大于 2。不管是什么我将它设置为,它只被调用两次。我已经有了必要的 if 语句来根据单元格索引等选择单元格原型(prototype)……所以这不是问题。问题是 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()

}

最佳答案

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

您要为 heightForRowAtIndexPath 返回什么。

关于ios - 使用超过 1 个 Prototype Cell 时 UITableView 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28288678/

相关文章:

ios - 使用 Storyboard和 plist 文件将 uitableview 单元格用作按钮

swift - 获取在 scn 文件中创建的球体的位置

ios - 核心蓝牙充当信标

iOS动画 block : ignores animation commands

ios - 如何告诉 UITableView 自调整单元格的高度即将改变?

ios - 如何在 iOS 视频播放器中播放 webm 文件

ios - Tableview 中的 CollectionView,数据源错误

ios - 带有 UIImage 或远程 URL 的 UNNotificationAttachment

ios - 使用自动布局在调整大小时平均更改空间

ios - 如何从仅顶部获取 UIView 上的阴影? [ objective-c ]