我的问题是当我滚动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
}
最佳答案
创建UITableViewCell
子类并覆盖prepeareForReuse
函数-将单元格设置为默认/必需模式。
迅速:
override func prepareForReuse() {
super.prepareForReuse()
//set cell to initial/required state here
}
准备一个可重用的单元以供表视图的委托重用。
如果
UITableViewCell
对象是可重用的(即,它具有重用标识符),则在从UITableVie
w方法dequeueReusableCellWithIdentifier:
返回对象之前,将调用此方法。出于性能原因,您只应重置与内容无关的单元格属性,例如,alpha,编辑和选择状态。复用单元格时,tableView:cellForRowAtIndexPath:
中的表视图的委托应始终重置所有内容。如果单元对象没有关联的重用标识符,则不会调用此方法。如果重写此方法,则必须确保调用 super class 实现。关于ios - TableView无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38612826/