2011-12-19 6 views
0

これまでのところ、スタックオーバーフローで検索していますが、私のような状況が見つかりませんでした。どんな援助も非常に高く評価されています。私は、人物Aにチェックマークを付けると、人物Hも1人も持っていて10人ほど離れていることに気づいています。基本的に10回毎にチェックマークが繰り返されます。ここでチェックマークが付いたチェックマーク付きのUITableViewCell

は私のコードです:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UITableViewCell *cell; 
cell = [self.tableView cellForRowAtIndexPath: indexPath]; 

if ([[myArrayOfAddressBooks objectAtIndex:indexPath.row] objectForKey:@"emailSelected"] != @"YES") 
{  
cell.accessoryType = UITableViewCellAccessoryCheckmark; 
[[myArrayOfAddressBooks objectAtIndex:indexPath.row] setValue:@"YES" forKey:@"emailSelected"]; 
} 
else 
{  
    cell.accessoryType = UITableViewCellAccessoryNone; 
    [[myArrayOfAddressBooks objectAtIndex:indexPath.row] setValue:@"NO" forKey:@"emailSelected"]; 
}  

答えて

6

これはUITableViewUITableViewCellを "リサイクル" する方法によるものである:私は

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

{static NSString *CellIdentifier = @"MyCell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 

      } 

cell.textLabel.text = 

[NSString stringWithFormat:@"%@ %@", [[myArrayOfAddressBooks objectAtIndex:indexPath.row] objectForKey:@"FirstName"],[[myArrayOfAddressBooks objectAtIndex:indexPath.row] objectForKey:@"LastName"]]; 
cell.detailTextLabel.text = 

[NSString stringWithFormat:@"%@", [[myArrayOfAddressBooks objectAtIndex:indexPath.row] objectForKey:@"Address"]]; 

return cell; 

}

私はこれを持っていたインデックスパスのための行選択をしましたセルを選択したときのマーキング方法について説明します。

tableView:cellForRowAtIndexPath:内で処理/作成するすべてのセルに対して、accessoryTypeの値をリフレッシュまたは設定する必要があります。あなたは正しくmyArrayOfAddressBooksデータ構造の状態を更新し、あなただけの@"Yes"または@"No"文字列として状態を保存するための十分な理由がない限り、またtableView:cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

{ 
    static NSString *CellIdentifier = @"MyCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    NSDictionary *info = [myArrayOfAddressBooks objectAtIndex:indexPath.row]; 

    cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", [info objectForKey:@"FirstName"],[info objectForKey:@"LastName"]]; 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", [info objectForKey:@"Address"]]; 

    cell.accessoryType = ([[info objectForKey:@"emailSelected"] isEqualString:@"YES"]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; 

    return cell; 
} 

にこの情報を使用する必要があり、それらを保存しない理由[NSNumber numberWithBool:YES]または[NSNumber numberWithBool:NO]isEqualToString:を常時使用するのではなく、比較したいときは、ロジックを簡素化します。

cell.accessoryType = ([[info objectForKey:@"emailSelected"] boolValue]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; 
関連する問題