Swift中的UITableView的奇怪行为 [英] Odd behavior with UITableView in Swift

查看:76
本文介绍了Swift中的UITableView的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个标签和一个图像的自定义单元格.我从Json的互联网上收到了一些数据.一切正常.每个单元格都填充相应的数据.我添加了一个新标签,该标签必须像其他标签一样填写.这是数据:

I have a custom cell with two labels and a image. I receive some data from internet in Json. Everything works fine; every cell fills with the correspondent data. I've added a new label that has to be filled just like the other ones. This is the data:

let cell = tableView.dequeueReusableCellWithIdentifier("friendCell") as! friendCell

cell.friendPicture?.image = newImageUser

cell.friendName.text = "@\(theName[indexPath.row])"

if repliesNumber[indexPath.row] == "0"{

        cell.repliesNumber.textColor = UIColor.redColor()
    }
    else{

        cell.repliesNumber.textColor = UIColor(patternImage: UIImage(named:"backgroundPattern")!)
    }

if AttachedImage[indexPath.row] != ""{

        cell.backgroundColor = .orangeColor()

    }

为了进行测试,我对前两个单元进行了着色.我的问题是,拳头的两个单元会变色,但是如果我向下滚动,每两个,三个,四个单元(取决于设备(我猜是设备的高度),另外两个单元也会变色.

For testing I've made to be colored the first two cells. My issue is that the fist two cells get colored, but if I scroll down, every two, three, four cells (depending on the device -device height I guess-, another two cells get colored too.

这很奇怪/奇怪,因为其余标签工作正常.

It's odd/weird because the rest of labels work fine.

我应该从哪里开始寻找?

Where should I start looking?

如果我打印json数据,我收到的一切都很好

If I print the json data I receive everything is okay

这是一个gif:

GIF

基本上我的问题是,当我向下滚动时,数据消失了,但仅从标签消失了,其余的标签和图像却没有.

Basically my problem is that when I scroll down the data disappear but only from a label, the rest of labels and images doesn't.

推荐答案

从您的代码中不是很清楚哪些单元格是彩色的".是来自orangeColor()条件吗?

It's not very clear from your code which are the cells that are "colored". Is it from the orangeColor() condition ?

无论如何,UITableView中的UITableViewCells被重用,这意味着您将获得与它们完全相同的状态.这就是为什么,如果不重置backgroundColor,则在滚动时它们仍然是橙色的.

Anyway, UITableViewCells in an UITableView are reused, which means you are given them in the exact same state you left them. This is why, if you don't reset your backgroundColor, they still are orange as you scroll.

因此,基本上,每当您在单元格中的特定条件下执行某项操作时,请不要忘记在不满足条件时恢复其状态.您的情况如下:

So basically, whenever you do something in a certain condition in a cell, don't forget to restore its state when the condition is not met. In your case, this gives :

// cellForRowAtIndexPath {
if itemIsMultimedia() {
    cell.Multimedia.text = "it's multimedia"
    cell.Multimedia.hidden = false
}
else {
    cell.Multimedia.text = ""
    cell.Multimedia.hidden = true
}

这篇关于Swift中的UITableView的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆