我正在创建一个显示用户列表的视图。当前用户能够单击其中一个单元格,从而跟随或取消该单元格中的用户。出于某种原因,复选标记显示在不应具有复选标记的单元格中(意味着当前用户跟随某人他实际上不是)。用户的图像也不正确。我想这的原因与复选标记错误有关。我做错了什么?请帮助!
import UIKit
import Parse
class TableViewController: UITableViewController {
var refresher: UIRefreshControl!
var usernames = [""]
var userIds = [""]
var userPics = [String:PFFile]()
var isFollowing = ["":false]
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.hidesBackButton = true
refresher = UIRefreshControl()
refresher.attributedTitle = NSAttributedString(string: "Pull to refresh")
refresher.addTarget(self, action: "refresh", forControlEvents: UIControlEvents.ValueChanged)
self.tableView.addSubview(refresher)
refresh()
}
func refresh (){
let query = PFUser.query()
query?.findObjectsInBackgroundWithBlock({ (object, error) -> Void in
if let users = object {
self.usernames.removeAll(keepCapacity: true)
self.userIds.removeAll(keepCapacity: true)
self.isFollowing.removeAll(keepCapacity: true)
self.userPics.removeAll(keepCapacity: true)
for objects in users {
if let user = objects as? PFUser {
if user.objectId != PFUser.currentUser()?.objectId {
self.usernames.append(user.username!)
self.userIds.append(user.objectId!)
if let image = user["profileImage"] {
self.userPics[user.objectId!] = image as? PFFile
}
let query = PFQuery(className: "Followers")
query.whereKey("following", equalTo: user.objectId!)
query.whereKey("follower", equalTo: (PFUser.currentUser()!.objectId)!)
query.findObjectsInBackgroundWithBlock({ (object, error) -> Void in
if let object = object {
if object.count > 0 {
self.isFollowing[user.objectId!] = true
} else {
self.isFollowing[user.objectId!] = false
}
}
if self.isFollowing.count == self.usernames.count {
print(self.isFollowing)
print(self.userIds)
self.tableView.reloadData()
self.refresher.endRefreshing()
}
})
}
}
}
}
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return usernames.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UsersTableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UsersTableViewCell
cell.userLabel.text = usernames[indexPath.row]
if isFollowing[userIds[indexPath.row]] == true {
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
}
cell.userImage.frame = CGRectMake(0, 0, 100, 100)
cell.userImage.clipsToBounds = true
cell.userImage.layer.cornerRadius = cell.userImage.frame.height/2
if userPics[userIds[indexPath.row]] != nil {
userPics[userIds[indexPath.row]]!.getDataInBackgroundWithBlock { (data, error) -> Void in
if let data = data {
cell.userImage.image = UIImage(data: data)
}
}
}
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell: UsersTableViewCell = tableView.cellForRowAtIndexPath(indexPath)! as! UsersTableViewCell
if isFollowing[userIds[indexPath.row]] == false {
isFollowing[userIds[indexPath.row]] = true
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
let following = PFObject(className: "Followers")
following["following"] = userIds[indexPath.row]
following["follower"] = PFUser.currentUser()?.objectId
following.saveInBackground()
} else {
isFollowing[userIds[indexPath.row]] = false
cell.accessoryType = UITableViewCellAccessoryType.None
let query = PFQuery(className: "Followers")
query.whereKey("following", equalTo: userIds[indexPath.row])
query.whereKey("follower", equalTo: (PFUser.currentUser()?.objectId)!)
query.findObjectsInBackgroundWithBlock({ (object, error) -> Void in
if let object = object {
for users in object {
users.deleteInBackground()
}
}
})
}
}
}
答案 0 :(得分:0)
像迈克尔说的那样,你需要清除复选标记,尝试使用此代码而不是if
cell.accessoryType = isFollowing[userIds[indexPath.row]] ? UITableViewCellAccessoryType.Checkmark : UITableViewCellAccessoryType.None