gpt4 book ai didi

ios - tableView 中的搜索和复选标记

转载 作者:行者123 更新时间:2023-11-29 03:24:36 24 4
gpt4 key购买 nike

我正在尝试在 iOS 中创建一个搜索栏,并将其设置到过滤结果的位置,然后当您单击它时,它会显示一个复选标记。当我删除搜索文本时,复选标记消失,整页单元格出现,但我在搜索中选择的单元格未选中带有复选标记。有人可以修复下面的代码来帮助我吗?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableSet *selectedUsers = [NSMutableSet set];
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];


PFUser *user;
if (self.userResults != nil)
{
user = [self.userResults objectAtIndex:indexPath.row];
}
else
{
user = [self.allUsers objectAtIndex:indexPath.row];
}
cell.textLabel.text = user.username;


if ([selectedUsers containsObject:user])
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
if ([selectedUsers containsObject:user])
{
// user already selected

}
else
{
// select user
[selectedUsers addObject:user];
}


return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

cell.accessoryType = UITableViewCellAccessoryCheckmark;
PFRelation *friendsRelation = [self.currentUser relationforKey:@"friendsRelation"];
PFUser *user = [self.allUsers objectAtIndex:indexPath.row];
[friendsRelation addObject:user];
[self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
NSLog(@"Error %@ %@", error, [error userInfo]);

} }];






}

最佳答案

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:    (NSIndexPath *)indexPath
{
NSMutableSet *selectedUsers = [NSMutableSet set];
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
PFUser *user;
if (self.userResults != nil)
{
user = [self.userResults objectAtIndex:indexPath.row];
}
else
{
user = [self.allUsers objectAtIndex:indexPath.row];
}
cell.textLabel.text = user.username;
if ([selectedUsers containsObject:user])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];


PFRelation *friendsRelation = [self.currentUser relationforKey:@"friendsRelation"];
PFUser *user = [self.allUsers objectAtIndex:indexPath.row];
[friendsRelation addObject:user];
[self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
NSLog(@"Error %@ %@", error, [error userInfo]);

} }];

PFUser *user1;
if (self.userResults != nil)
{
user1 = [self.userResults objectAtIndex:indexPath.row];
}
else
{
user1 = [self.allUsers objectAtIndex:indexPath.row];
}
if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
{
cell.accessoryType = UITableViewCellAccessoryNone;
[selectedUsers removeObject:user1];
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[selectedUsers addObject:user1];
}

}

看看这是否有帮助。

关于ios - tableView 中的搜索和复选标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20626196/

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