在搜索如何使UITableViewCellAccessoryCheckmark成为正确的单元格时?

时间:2016-02-29 07:38:16

标签: arrays uisearchbar checkmark uitableview

我有一个带有2个数组的视图控制器,一个用于正常结果;另一个是基于文本输入的过滤/搜索结果。

一次只能有一个单元格拥有UITableViewCellAccessoryCheckmark。

我的问题可以这样描述;

IE:

  1. 视图控制器被送入Venue对象;并标有 UITableViewCellAccessoryCheckmark。这是预期的,也是正确的。
  2. 搜索查询中的用户类型,使用搜索结果数组; 但是UITableViewCellAccessoryCheckmark不再出现在 之前在步骤1中检查过的地点。
  3. 我不确定为什么不检查细胞。

    视觉示例;

    原始视图。 Cobo Arena是预先选定的地点

    First view

    -

    打字时;选中标记不在正确的位置

    Searching filters the results

    -

    更多输入:复选标记现已消失

    More typing

    -

    代码在

    之下
    - (void) configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
    {
        FCVenue *venue;
    
        if (self.isSearching)
        {
            venue = [self.searchResults objectAtIndex:indexPath.row];
        }
        else
        {
            venue = [self.venues objectAtIndex:indexPath.row];
        }
    
        if ([indexPath isEqual:self.selectedIndexPath])
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    
        cell.textLabel.text = [NSString stringWithFormat:@"%@", venue.name];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", venue.location];
    }
    
    -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [self.tableView cellForRowAtIndexPath:self.selectedIndexPath].accessoryType = UITableViewCellAccessoryNone;
        self.selectedIndexPath = indexPath;
        [self.tableView reloadRowsAtIndexPaths:@[self.selectedIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    
        return indexPath;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
        FCVenue *venue;
    
        if (self.searching)
        {
            venue = [self.searchResults objectAtIndex:indexPath.row];
        }
        else
        {
            venue = [self.venues objectAtIndex:indexPath.row];
        }
    
        self.selectedVenue = venue;
    
    // This method fires a completion block and dismisses the view controller
        if (self.completionBlock)
        {
            self.completionBlock( self, self.selectedVenue );
        }
    
        [self.navigationController dismissViewControllerAnimated:YES completion:^{
        }];
    }
    

1 个答案:

答案 0 :(得分:1)

这种情况正在发生,因为您正在存储完整表的索引以显示复选标记。相反,您应该比较FCVenue对象以查看是否是已检查的对象。

所以代码应该是这样的,但它没有经过测试:

- (void) configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    FCVenue *venue;

    if (self.isSearching)
    {
        venue = [self.searchResults objectAtIndex:indexPath.row];
    }
    else
    {
        venue = [self.venues objectAtIndex:indexPath.row];
    }

    if ([venue isEqual:self.selectedVenue]) // You may want to compare just the id or any other unique property
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        // if you opt for keeping the selectedIndexPath property you need to refresh it here.
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    cell.textLabel.text = [NSString stringWithFormat:@"%@", venue.name];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", venue.location];
}

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   //[self.tableView cellForRowAtIndexPath:self.selectedIndexPath].accessoryType = UITableViewCellAccessoryNone;
    // Here you may want to do a loop over all the possible index path to clean the state or keep storing the selected indexPath just to clear the mark when the selected venue changes.
    FCVenue *venue;

    if (self.searching)
    {
        venue = [self.searchResults objectAtIndex:indexPath.row];
    }
    else
    {
        venue = [self.venues objectAtIndex:indexPath.row];
    }

    self.selectedVenue = venue;
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

    return indexPath;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];



// This method fires a completion block and dismisses the view controller
    if (self.completionBlock)
    {
        self.completionBlock( self, self.selectedVenue );
    }

    [self.navigationController dismissViewControllerAnimated:YES completion:^{
    }];
}

在任何情况下,一般的想法是你需要将场地与支票联系起来,而不是在indexPath上,因为indexPath会随着搜索而改变。