2012-01-12 6 views
0

UITableViewから行を選択すると、その行とその下にある行(選択した行の下にある複数行)も選択されます。選択された行のみが選択された行になると予想されます。IPhone:別の行が選択されている

私のコードは次のとおりです。事前に

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 
    //Deselect 
    cell.accessoryType = UITableViewCellAccessoryNone; 
    cell.backgroundColor=[UIColor clearColor]; 
} else { 
    //Select 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    cell.backgroundColor=[UIColor redColor]; 
} 
} 

ありがとう!

答えて

0

はい、あなたは新しいを宣言する必要があなたのデータソース数のNSMutableArray(たとえば_selectedListviewDidLoad又はinit方法、

_selectedList = [[NSMutableArray alloc] init]; 
for(int i = 0; i < [datasource count]; i++) 
{ 
    [_selectedList addObject:[NSNumber numberWithBool:NO]]; 
} 

において値0

(クラスメンバーとして).hファイルで宣言NSMutableArray *_selectedList;

とのNSNumberを移入し、次のように以下の方法を作ります。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //... 
    if (! [[_selectedList objectAtIndex:indexPath.row] boolValue]) { 
     //Deselected 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     cell.backgroundColor=[UIColor clearColor]; 
    } else { 
     //Selected 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     cell.backgroundColor=[UIColor redColor]; 
    } 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 
    //Deselect 
    cell.accessoryType = UITableViewCellAccessoryNone; 
    cell.backgroundColor=[UIColor clearColor]; 
    } else { 
    //Select 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    cell.backgroundColor=[UIColor redColor]; 
    } 
    BOOL isSelected = ![[_selectedList objectAtIndex:indexPath.row] boolValue]; 
    [_selectedList replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:isSelected]]; 
} 
+0

それは動作します!ありがとう! –

2

これはおそらく、セルが再利用されたためです。 あなたが選択した状態を示すために、背景色を使用する場合は、このコードを追加するセルgeter方法で

それを設定する必要が動作するはずです:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //... 
    if (!cell.selected) { 
     //Deselected 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     cell.backgroundColor=[UIColor clearColor]; 
    } else { 
     //Selected 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     cell.backgroundColor=[UIColor redColor]; 
    } 

} 
+0

選択された一つ下の行の一部(私はそれらを見るためにスクロールする必要が)選択しておくとに、私は戻って、選択にスクロールし、このいずれかを選択することはできませんが、近いものです。これはおかしい。私は別の方法で作ろうとします –

関連する問題