Ios TableView工作不正常
我的问题是当我滚动tableview时,它不能正常工作 例如,我有2个部分,在第0部分中,我在单元格中使用配置文件图像,在第1部分中,我隐藏它们,但当我向下和向上滚动时,图像丢失。我还有一个变量,当它正常时,我会改变第二个图像。但当我滚动它不断变化的图像时,但当我向下滚动它不断变化的图像时,这等于 你可以在这里找到图片Ios TableView工作不正常,ios,swift,uitableview,Ios,Swift,Uitableview,我的问题是当我滚动tableview时,它不能正常工作 例如,我有2个部分,在第0部分中,我在单元格中使用配置文件图像,在第1部分中,我隐藏它们,但当我向下和向上滚动时,图像丢失。我还有一个变量,当它正常时,我会改变第二个图像。但当我滚动它不断变化的图像时,但当我向下滚动它不断变化的图像时,这等于 你可以在这里找到图片 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIn
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
子类并覆盖prebeareforeuse
函数-将单元格转换为默认/必需模式
斯威夫特:
override func prepareForReuse() {
super.prepareForReuse()
//set cell to initial/required state here
}
准备一个可重用的单元格,以供表视图的委托重用
如果一个UITableViewCell
对象是可重用的,也就是说,它有一个重用标识符,那么在从UITableVie
w方法dequeueReusableCellWithIdentifier:
返回对象之前,将调用此方法。出于性能原因,您应该只重置与内容无关的单元格属性,例如alpha、编辑和选择状态。在重用单元格时,tableView:cellforrowatinexpath:
中的表视图委托应始终重置所有内容。如果单元对象没有关联的重用标识符,则不会调用此方法。如果重写此方法,则必须确保调用超类实现
在这里添加
cellForRowAtIndexPath
的全部代码。让user=self.users[indexPath.row]代替indexPath.row。在使用分段表视图时,您应该执行indexPath.section。