Checkmark自动添加到tableView IOS中未选中的行

时间:2016-06-02 10:41:48

标签: ios swift uitableview tableview

我遇到了一个非常奇怪的问题。我正在尝试创建一个像instagram这样的小应用程序,然后使用Parse作为后端。

到目前为止我做了什么:

  • 用户可以注册,登录并互相关注
  • 当用户登录时,会显示其他用户的列表,用户可以通过选择用户名来关注/取消关注用户。
  • 当用户关注时,会出现复选标记,当用户取消关闭时,复选标记消失

问题

我面临的问题是,当用户跟随​​其他用户时,此时未出现在表格中的其他用户(由于无法进入屏幕而被隐藏)也会被检查/选中但未被跟踪。请查看我附上的屏幕截图以获取更多详细信息。

Image 2

Image 3

代码:

import UIKit
import Parse

var userNames = [""];
var userIds = [""];
var isFollowing = ["" : false]
class TableViewController: UITableViewController {
override func viewDidLoad() {
    super.viewDidLoad()
    var query = PFUser.query();

    query?.findObjectsInBackgroundWithBlock({ (objects, errors) in

        if errors != nil{
            print("Error occurred while querying");
            print(errors)
        } else{
            userNames.removeAll(keepCapacity: true);
            userNames.removeAll(keepCapacity: true);
            isFollowing.removeAll(keepCapacity: true);
            print("User Details will come");
            for object in objects!{

                if object.objectId != PFUser.currentUser()?.objectId{
                    if let userDetails = object as? PFUser{
                        userNames.append(userDetails.username!);
                        userIds.append(userDetails.objectId!);

                        var query = PFQuery(className: "followers");
                        query.whereKey("follower", equalTo: (PFUser.currentUser()?.objectId)!);
                        query.whereKey("following", equalTo: userDetails.objectId!);

                        query.findObjectsInBackgroundWithBlock({ (objects, error) in
                            if let objects = objects {

                                if (objects.count > 0){
                                    isFollowing[userDetails.objectId!] = true;
                                } else{
                                    isFollowing[userDetails.objectId!] = false;
                                }
                            }
                            if isFollowing.count == userNames.count {
                                self.tableView.reloadData();
                            }
                        })
                    }
                }
            }
        }     
    })
}



override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    print(indexPath.row)
    let followedObjectID = userIds[indexPath.row]
    var cell : UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
    if isFollowing[followedObjectID] == false{
        isFollowing[followedObjectID] = true;

        var  following  = PFObject(className: "followers");
        following["following"] = userIds[indexPath.row]; //This will put the objectId of the individual followed in the following field.
        following["follower"] = PFUser.currentUser()?.objectId; // This will put the ObjectId of current loggedIn individual in the follower field.
        following["followingName"] = userNames[indexPath.row];
        following["followerName"] = PFUser.currentUser()?.username;
        following.saveInBackground();

        cell.accessoryType = UITableViewCellAccessoryType.Checkmark;
    } else{
        isFollowing[followedObjectID] = false;
        cell.accessoryType = UITableViewCellAccessoryType.None;

        var query = PFQuery(className: "followers");
        query.whereKey("following", equalTo: userIds[indexPath.row]);
        query.whereKey("follower", equalTo: (PFUser.currentUser()?.objectId)!)
        query.findObjectsInBackgroundWithBlock({ (objects, error) in

            if let objects = objects{
                for object in objects{
                    object.deleteInBackground();
                }
            }
        })
    }

}


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) -> UITableViewCell {
    let followedObjectID = userIds[indexPath.row];
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

    cell.textLabel?.text = userNames[indexPath.row];
    if (isFollowing[followedObjectID] == true){
        cell.accessoryType = UITableViewCellAccessoryType.Checkmark;
    } else{
        //print("short term lolum");
    }
    return cell
}
}

2 个答案:

答案 0 :(得分:0)

请在数据源中添加一个键,通过设置值0/1来确定您是否选择了该行。

cellForRow中检查该属性的值并根据该显示附件视图。

didSelect中,根据行选择确保键值0到1和1到0。

答案 1 :(得分:0)

这是因为可重复使用的单元格,因此如果isFollowing [followObjectID] == false,则应删除复选标记,因此只需添加代码以删除else块中的复选标记: -

  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
       let followedObjectID = userIds[indexPath.row];
       let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

        cell.textLabel?.text = userNames[indexPath.row];
        if (isFollowing[followedObjectID] == true){
          cell.accessoryType = UITableViewCellAccessoryType.Checkmark;
        } else {
          cell.accessoryType = UITableViewCellAccessoryType.None
        }
      return cell
}