2016-07-27 52 views
1

我的問題是當我滾動tableview它不能正常工作。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 
} 

image1

image2

+0

添加'cellForRowAtIndexPath'的整個代碼。 –

+0

編輯問題 –

+0

讓用戶= self.users [** indexPath.row **]在這裏,而不是indexPath.row你應該做indexPath.section,因爲你有分段tableView。 –

回答

3

創建UITableViewCell子類並覆蓋prepeareForReuse功能 - 把電池爲默認/需要的模式。

斯威夫特:

override func prepareForReuse() { 
    super.prepareForReuse() 

    //set cell to initial/required state here 
} 

準備通過表視圖的委託重複使用可重複使用的電池。

如果一個UITableViewCell對象是可重用的 - 也就是說,它有一個重用標識符 - 這個方法在對象從UITableVie w方法dequeueReusableCellWithIdentifier:返回之前被調用。出於性能考慮,您應該只重置與內容無關的單元格屬性,例如,alpha,編輯和選擇狀態。 tableView:cellForRowAtIndexPath:中的表格視圖委託應該在重新使用單元時始終重置所有內容。如果單元對象沒有關聯的重用標識符,則不調用此方法。如果您重寫此方法,則必須確保調用超類實現。

+0

這就是解決我的問題,謝謝 –

+0

我很高興幫助! –