我的问题是当我滚动tableview时它无法正常工作。
例如我有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
}
答案 0 :(得分:3)
创建UITableViewCell
子类并覆盖prepeareForReuse
函数 - 将单元格设置为默认/所需模式。
夫特:
override func prepareForReuse() {
super.prepareForReuse()
//set cell to initial/required state here
}
准备一个可重用的单元格,供表视图的委托重用。
如果
UITableViewCell
对象是可重用的 - 也就是说,它具有重用标识符 - 在从UITableVie
w方法dequeueReusableCellWithIdentifier:
返回对象之前调用此方法。出于性能原因,您应该仅重置与内容无关的单元格属性,例如,alpha,编辑和选择状态。在tableView:cellForRowAtIndexPath:
中,表视图的委托应始终在重用单元格时重置所有内容。如果单元对象没有关联的重用标识符,则不会调用此方法。如果重写此方法,则必须确保调用超类实现。