ios - UITableview 原型(prototype)单元格未正确填充

标签 ios swift uitableview

我正在尝试使用“Facebook 状态”填充 UITableView 原型(prototype)单元。它包含图像和视频,具体取决于 Facebook 服务器的响应。但我无法正确填充它。

我将从数组中获取第一张图像。但是当我向下滚动 UITableView 并再次到达顶部时,图像消失了。当我打印 i 的值时,它在加载 TableView 后不久就超过了数组的计数。

我在此处添加编辑后的代码。

覆盖 func tableView(_ tableView: UITableView, numberOfRowsInSection 部分: Int) -> Int {

    var rows : Int = 0
    if section < numberOfRowsAtSection.count{
        rows = numberOfRowsAtSection[section]
        print(section,rows)
    }
    return rows
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    print("Section,Row",indexPath.section, indexPath.row)
    switch(combinedArray[indexPath.section][indexPath.row]){
    case "photo" :
        let cell = tableView.dequeueReusableCell(withIdentifier: "ImageDetail", for:indexPath) as! imageCell
            if(im < imageDetails.count){
                let imageUrl = imageDetails[im]
                let url = URL(string:imageUrl)
                let data = try? Data(contentsOf: url!)
                cell.storyDetailsLbl1.text = photoStoryDetails[im]
                print(imageDetails[im])
                cell.images1.image = UIImage(data: data!)
            }
            im = im + 1

            return cell

    case "video" :
            let cell1 = tableView.dequeueReusableCell(withIdentifier: "VideoCell", for: indexPath) as! videoPlayerCell
            cell1.videoView.isHidden = false
            if  vd < videoURLs.count{
                print(videoURLs.count)
                let urls = videoURLs[vd]
                cell1.videoPlayerItem = AVPlayerItem.init(url: urls)
                cell1.videoLabel.text = videoStoryDetails[vd]
            }
            vd = vd + 1

            return cell1

    case "link":
            let cell2 = tableView.dequeueReusableCell(withIdentifier: "LinkCell", for: indexPath) as! linkCell
            if  ln < linkDetails.count {
                cell2.linkLbl.text = linkDetails[ln]
            }
            ln = ln + 1
            return cell2

    default:
             let cell3 = tableView.dequeueReusableCell(withIdentifier: "StatusCell", for: indexPath) as! statusCell
                if  st < statusDetails.count{
                    cell3.statusLbl.text = statusDetails[st]
                }
                st = st + 1
                return cell3
    }

}

最佳答案

Tableviews deque 已经创建了单元格并重用它们以提高性能。因此,可以重复使用标识符为“ImageDetail”的单元格,并使用任何案例语句中的数据填充该单元格。这会导致一些奇怪的现象,比如图像在不应该消失的时候消失了。

解决此问题的一种方法是确保在每种情况下都处理每个单元格属性。 cell.storyDe​​tailsLbl1.textphoto 情况下不会被处理。 cell.images1.imagelink 等情况下不被处理。

最好为每个案例定义一个具有唯一标识符的单元格。即 PhotoCellLinkCellVideoCell

关于ios - UITableview 原型(prototype)单元格未正确填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47060903/

相关文章:

ios - 如何在 crittercism 中使用崩溃日志找到崩溃?

ios - 将线性转换为 Apple Display P3 色彩空间的方程式,反之亦然

swift - 如何实例化由扩展完全定义的协议(protocol)的默认实现

iOS Swift - 加载自定义表格单元格

ios - UITableView:呈现的单元数

iphone:如何检查 uitableview 行模数

ios - iOS 7.1 中的 UISlider setMaximumTrackTintColor

ios - 将 UIImageView 的动画与过渡相结合

ios - 将类中的字符串元素附加到字符串数组

iphone - UITableView 中每个部分的重复元素