【问题标题】:Search and Checkmarks in tableViewstableViews 中的搜索和复选标记
【发布时间】:2013-12-17 04:55:15
【问题描述】:

我正在尝试在 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]);

    }    }];






}

【问题讨论】:

    标签: ios objective-c uitableview uisearchbar


    【解决方案1】:
      - (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];
    }
    
    }
    

    看看这是否有帮助。

    【讨论】:

    • g它仍然不起作用。它做同样的事情。
    • 为什么需要修改 cellForRowAtIndexPath 中的数组?你不能在 didSelectRowAtIndexPath 中做吗?
    • 我的猜测是 ios 删除 cellForRow 中的用户对象是造成问题的原因。
    • cellForRow 中的这个
    • 我已经把以上两种方法都放在了我原来的帖子里
    【解决方案2】:

    问题是,当您关闭搜索栏时,表格视图会重新加载。这次您必须记录用户选择的所有单元格索引路径。即您在 didSelectRowAtIndexPath 委托方法中执行此操作,并保存在数组值中该用户选择。

    在 cellForRowAtIndexPath 中,您可以检查数组中 indexPath 所在的所有单元格。

    希望你明白了...

    【讨论】:

      【解决方案3】:
       NSMutableArray *array=[[NSMutableArray alloc]init];
      
         - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if
         ([selectedBillsArray containsObject:indexPath])    {
                        cell.accessoryType=UITableViewCellAccessoryCheckmark;    }        
      
         }
      
         - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
             UITableViewCell *cell= [tableView cellForRowAtIndexPath:indexPath];
      
             if (cell.accessoryType==UITableViewCellAccessoryCheckmark)
             {
                 [cell setAccessoryType:UITableViewCellAccessoryNone];
      
                 [selectedBillsArray removeObject:[NSString stringWithFormat:@"%i",indexPath];
             }
             else
             {
                 [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
                [selectedBillsArray addObject:[NSString stringWithFormat:@"%i",indexPath];
             }
             }
      

      做这样的事情......

      【讨论】:

        猜你喜欢
        • 2018-02-16
        • 2010-09-17
        • 2019-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多