简体   繁体   中英

TableView Not Working Properly

My problem is when I scroll tableview its not working properly.

For example I have 2 section And in section 0 I'm using profile image in cell and in section 1 I'm hiding them but when I scroll down and up images are losing. also I have a variable and when its ok I'm changing second image. But when I scroll its changing image but when I scroll down its changing that images which is equal to indexpath.

You can find images in here

    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
}

此搜索

图像2

Create UITableViewCell subclass and override prepeareForReuse function - to turn cell to default/required mode.

Swift:

override func prepareForReuse() {
    super.prepareForReuse()

    //set cell to initial/required state here 
}

Prepares a reusable cell for reuse by the table view's delegate.

If a UITableViewCell object is reusable—that is, it has a reuse identifier—this method is invoked just before the object is returned from the UITableVie w method dequeueReusableCellWithIdentifier: . For performance reasons, you should only reset attributes of the cell that are not related to content, for example, alpha, editing, and selection state. The table view's delegate in tableView:cellForRowAtIndexPath: should always reset all content when reusing a cell. If the cell object does not have an associated reuse identifier, this method is not called. If you override this method, you must be sure to invoke the superclass implementation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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