繁体   English   中英

UITableview原型单元无法正确填充

[英]UITableview prototype cell not populating properly

我正在尝试使用“ Facebook状态”填充UITableView原型单元。 它具有图像和视频,具体取决于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
    }

}

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

解决此问题的一种方法是确保您在每种情况下都处理每个单元格属性。 photo盒中cell.storyDetailsLbl1.text cell.images1.imagelink情况下未处理,等等。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM