gpt4 book ai didi

ios - 选中标记自动添加到未在 tableView IOS 中选中的行

转载 作者:行者123 更新时间:2023-11-28 08:36:21 25 4
gpt4 key购买 nike

我遇到了一个非常奇怪的问题。我正在尝试制作一个像 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
}
}

最佳答案

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

  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
}

关于ios - 选中标记自动添加到未在 tableView IOS 中选中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37589551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com