首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >滚动时UITableView多节冲突

滚动时UITableView多节冲突
EN

Stack Overflow用户
提问于 2019-04-11 16:41:30
回答 2查看 93关注 0票数 0

关于UITableView有一个奇怪的问题。我有3个数据源,这意味着在UITableView中有3个部分。当我滚动UITableView时,按钮和图像发生冲突。按钮正在消失,图像正在变形。

这里是我的cellForRowAt方法。

代码语言:javascript
复制
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "eventnetworkingcell", for: indexPath) as! EventNetworkCell

        var name: String = ""
        var job: String = ""
        var company: String = ""
        var img: Int?

        cell.userImageView.layer.cornerRadius = 45
        cell.userImageView.clipsToBounds = true

        if indexPath.section == 0 {

            name = self.matchmaking[indexPath.row].name
            job = self.matchmaking[indexPath.row].job
            company = self.matchmaking[indexPath.row].company
            img = self.matchmaking[indexPath.row].image


        }
        else if indexPath.section == 1 {

            name = self.networkInEvent[indexPath.row].name
            job = self.networkInEvent[indexPath.row].job
            company = self.networkInEvent[indexPath.row].company
            img = self.networkInEvent[indexPath.row].image

            cell.addButtonOutlet.alpha = 0
        }
        else {

            name = self.allAttendees[indexPath.row].name
            job = self.allAttendees[indexPath.row].job
            company = self.allAttendees[indexPath.row].company
            img = self.allAttendees[indexPath.row].image


        }

        cell.nameLabel.text = name
        cell.jobLabel.text = job
        cell.companyLabel.text = company

        if let imgid = img {
            let url = MTApi.url(for: imgid, size: .normal)
            cell.userImageView.sd_setImage(with: url, placeholderImage: nil, options: [], completed: nil)
        }
}
        cell.addButtonOutlet.addTarget(self, action: 
  #selector(self.addNetwork(sender:)), for: .touchUpInside)

        return cell

    }

当我删除cell.addButtonOutlet.alpha = 0行时,按钮并没有消失。

有一个视频显示了这个问题:

Video

EN

回答 2

Stack Overflow用户

发布于 2019-04-11 16:47:47

你在细胞重用方面遇到了问题。

基本上,实际情况是,tableView创建的单元格与屏幕上可见的单元格一样多,一旦一个单元格从视图中滚动出来,它就会重用于dataSource中的另一个项目。

按钮似乎消失的原因是,您以前已经删除了按钮,但现在,当重用时,没有告诉单元格再次显示按钮。

解决这个问题很简单,只需添加:

代码语言:javascript
复制
cell.addButtonOutlet.alpha = 0

添加到您的0和2节(else块)。

图像也是一样,前一个图像会被保留,除非你告诉计算单元在需要的时候删除图像,所以只需添加以下内容:

代码语言:javascript
复制
if let imgid = img {
    let url = MTApi.url(for: imgid, size: .normal)
    cell.userImageView.sd_setImage(with: url, placeholderImage: nil, options: [], completed: nil)
} else {
    cell.userImageView.image = nil
}
票数 0
EN

Stack Overflow用户

发布于 2019-04-11 16:48:20

代码语言:javascript
复制
    var name: String = ""
    var job: String = ""
    var company: String = ""
    var img: Int?

    cell.userImageView.layer.cornerRadius = 45
    cell.userImageView.clipsToBounds = true
    cell.addButtonOutlet.alpha = 1   // add more
    if indexPath.section == 0 {
     ........

你应该研究一下UITableviewCell的重用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55628193

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档