我遇到了一个非常奇怪的问题。我正在尝试创建一个像instagram这样的小应用程序,然后使用Parse作为后端。
到目前为止我做了什么:
问题
我面临的问题是,当用户跟随其他用户时,此时未出现在表格中的其他用户(由于无法进入屏幕而被隐藏)也会被检查/选中但未被跟踪。请查看我附上的屏幕截图以获取更多详细信息。
代码:
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
}
}
答案 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
}