0

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

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

我的问题可以这样描述;

IE:

  1. 视图控制器被提供了一个场地对象;并用 UITableViewCellAccessoryCheckmark 标记。这是预期的,也是正确的。
  2. 用户输入搜索查询,使用搜索结果数组;但是 UITableViewCellAccessoryCheckmark 不再位于先前在步骤 1 中检查的 Venue 上。

我不确定为什么它不检查单元格。

视觉示例;

原始视图。Cobo Arena是预选的场地

第一视角

-

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

搜索过滤结果

-

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

更多打字

-

代码如下

- (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:^{
    }];
}
4

1 回答 1

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 会随着搜索而改变。

于 2016-02-29T13:49:30.990 回答