2013-02-10 48 views
0

我使用的是tableview,我正在對uitableview進行多重檢查。一切都進行的完美,我',得到正確的值,但當我滾動表視圖時,它丟失了複選標記圖像(使用默認複選標記沒有自定義圖像),但選定的值保留在陣列中...滾動的tableview從uitableview單元格中移除checkmarrk

滾動表視圖刪除複選標記圖像。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    AppDelegate *app= (AppDelegate *)[[UIApplication sharedApplication]delegate]; 

    if([_tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark){ 

    NSLog(@"yes"); 

    [placesvisitedarray removeObject:[app.nameArray objectAtIndex:indexPath.row]]; 


    [_tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; 

    } 
    else 
    { 
     NSLog(@"no"); 

     [_tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; 

     [placesvisitedarray addObject:[app.nameArray objectAtIndex:indexPath.row]]; 

    } 
    // [_tableView reloadData]; 

} 

回答

1

的對勾您滾動實現代碼如下cellForRowAtIndexPath:被稱爲和細胞重建正因爲去除。

你可以寫檢查的方法,如果你的數組中存在一定值:

- (BOOL)stringExistsInPlacesVisited:(NSString *)stringToMatch { 
    for (NSString string in placesvisitedarray) { 
     if ([string isEqualTo:stringToMatch]) 
      return YES; 
    } 
    return NO; 
} 

然後在cellForRowAtIndexPath:你必須檢查placesvisitedarray和插入/刪除對號。

if ([stringExistsInPlacesVisited:[app.nameArray objectAtIndex:indexPath.row]) 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
else 
    cell.accessoryType = UITableViewCellAccessoryNone; 

代碼沒有經過測試,因此可能無法正常工作,但至少它會給你如何進行的想法。

+0

您能否爲此提供一個代碼 – 2013-02-10 11:42:20

+0

我試過了,但它根本沒有奏效 – 2013-02-10 13:07:43