2016-02-29 36 views
0

我有一個視圖控制器2個數組,一個用於正常結果;另一個是基於文本輸入的過濾/搜索結果。當搜索如何使UITableViewCellAccessoryCheckmark正確的單元格?

一次只有一個單元格可以具有UITableViewCellAccessoryCheckmark。

我的問題可以這樣描述;

IE:

  1. 視圖控制器被送入一個地點對象;並用 UITableViewCellAccessoryCheckmark標記。這是預期的,並且是正確的。
  2. 用戶類型在搜索查詢中,使用搜索結果數組; 但是UITableViewCellAccessoryCheckmark是先前在步驟1中

檢查 地點不再是我不知道爲什麼它不檢查電池。

Visual examples;

原始視圖。 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

這是因爲您正在存儲整個表的索引以顯示覆選標記。相反,您應該比較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將隨搜索而改變。