[swift]表不能正常工作

发布时间: 2017/3/6 23:28:33
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我的问题是当我滚动表其不能正常工作。

例如我有 2 节和节中 0 我在单元格中使用配置文件映像节 1 我躲他们而在向上和向下滚动时失去了图像。我也有一个变量和第二图像时我会改变其确定。但当我滚动其不断变化的图像,但当我向下其改变这滚动图像的等于 indexpath。

你可以在这里找到图像

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! UserTableViewCell
    if self.itemInfo.title == "USERNAME" && indexPath.section == 0 {
        let user = self.users[indexPath.row]
        let friendImage = UIImage(named: "SearchPeopleAdded")
        for friend in self.friends {
            if friend.objectId == user.objectId {
                cell.groupAddImageView.image = friendImage
                cell.selectionStyle = .None
                cell.userInteractionEnabled = false
            }
        }

        cell.nameLabel!.text = user["username"] as? String

        if let image = user["avatar_image"] as? PFFile {
            cell.userImageView.sd_setImageWithURL(NSURL(string: image.url!))
        }

    }else if itemInfo.title == "ADDRESSBOOK" && indexPath.section == 1 {
        if isSearchActive == false {
            let person = contacts[indexPath.row]
            var firstName = person.name?.firstName
            var lastName = person.name?.lastName

            if firstName == nil || lastName == nil {
                if firstName == nil {
                    firstName = ""
                }
                if lastName == nil {
                    lastName = ""
                }
                if firstName == nil && lastName == nil {
                    if person.phones![0].number != nil {
                        firstName = person.phones![0].number
                        lastName = ""
                    }else {
                        firstName = ""
                        lastName = ""
                    }
                }
            }
            let fullName: String = firstName! + " " + lastName!
            cell.nameLabel.text = fullName
            cell.userImageView.hidden = true
        }else {
            let person = filteredAddressBookUsers[indexPath.row]
            var firstName = person.name?.firstName
            var lastName = person.name?.lastName

            if firstName == nil || lastName == nil {
                if firstName == nil {
                    firstName = ""
                    print("first name nil")
                }
                if lastName == nil {
                    lastName = ""
                    print("last name nil")
                }
                if firstName == nil && lastName == nil {
                    print("both nil")
                    if person.phones![0].number != nil {
                        firstName = person.phones![0].number
                        lastName = ""
                    }else {
                        firstName = ""
                        lastName = ""
                    }

                }
            }
            let fullName: String = firstName! + " " + lastName!
            cell.nameLabel.text = fullName
            cell.userImageView.hidden = true
        }
    }else if self.itemInfo.title == "ADDRESSBOOK" && indexPath.section == 0 {
        let user = self.addressMatchedUsers[indexPath.row]

        let friendImage = UIImage(named: "SearchPeopleAdded")
        for friend in self.friends {
            if friend.objectId == user.objectId {
                cell.groupAddImageView.image = friendImage
                cell.selectionStyle = .None
                cell.userInteractionEnabled = false
            }
        }

        cell.nameLabel!.text = user["username"] as? String

        if let image = user["avatar_image"] as? PFFile {
            cell.userImageView.sd_setImageWithURL(NSURL(string: image.url!))
        }
    }
    //configure(cell, forRowAtIndexPath: indexPath)
    return cell
}

image1

image2

解决方法 1:

创建 UITableViewCell 子类和重写 prepeareForReuse 函数-可以将单元格默认所需模式。

Swift:

override func prepareForReuse() {
    super.prepareForReuse()

    //set cell to initial/required state here 
}

为表视图委托所重用准备一个可重用的单元格。

如果 UITableViewCell 是可重用的对象 — — 那就是,它有一个重用标识符 — — 从返回的对象之前调用此方法 UITableVie w 方法 dequeueReusableCellWithIdentifier: 。出于性能方面的考虑,您应该只重置属性的单元格,不涉及内容,例如、 阿尔法、 编辑和选择状态。在表视图委托 tableView:cellForRowAtIndexPath: 应始终重置所有内容重用单元格时。如果单元格对象没有关联的重用标识符,将不会调用此方法。如果您重写此方法,您必须确保调用父类的实现。

官方微信
官方QQ群
31647020