2010-04-15 75 views
1

問題實現一個UITableView,允許多行選擇

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

if (cell.accessoryType == UITableViewCellAccessoryNone) { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 
else { 
    cell.accessoryType = UITableViewCellAccessoryNone; 
} 

[tableView deselectRowAtIndexPath:indexPath animated:YES]; 

}

...修改每6個單元而不是隻選定行的「accessoryType」。我錯過了什麼?

感謝

更新:這裏是小區創建代碼...

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

    static NSString *TC = @"TC"; 

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

    NSUInteger row = [indexPath row]; 
    cell.textLabel.text = [NSString stringWithFormat:@"Person %d", row+1]; 

    return cell; 
} 

我的解決方案基於以下明顯的答案是...

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

    // EVERY row gets its one Identifier 
    NSString *TC = [NSString stringWithFormat: @"TC%d", indexPath.row]; 

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

    NSUInteger row = [indexPath row]; 
    cell.textLabel.text = [NSString stringWithFormat:@"Person %d", row+1]; 

    return cell; 
} 

如果有是我耳熟能詳的一種方式。如果我們可以根據在某一天傳遞的NSIndexPath更改特定單元格(至少對我來說看起來更加直觀),那將會很好。

回答

0

是的,只是想通了。這些是相同的單元格(下面的日誌是NSLog(@「%d%@」,row,cell))。

2010-04-15 12:43:40.722 ...[37343:207] 0 <MyListCell: 0x124bee0; baseClass = UITableViewCell; frame = (0 0; 320 44); autoresize = RM+TM; layer = <CALayer: 0x124c3a0>> 

2010-04-15 12:43:47.827 ...[37343:207] 10 <MyListCell: 0x124bee0; baseClass = UITableViewCell; frame = (0 31; 340 44); autoresize = W; layer = <CALayer: 0x124c3a0>> 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: PlayerTableViewCell]; 

dequeueReusableCellWithIdentifier使用已經創建的單元格,如果前一個單元格不在屏幕中。所以,你應該避免使用「dequeueReusableCellWithIdentifier」 - 創建一個字典NSIndexPath - > UITableViewCell並使用它來代替dequeueReusableCellWithIdentifier。

ADDED: 還可以檢查(ROWNUMBER + 1)在一winningNumbers數組存在,在一個替換的tableView附件:的cellForRowAtIndexPath:方法。這也應該工作

+1

是不是對的最佳做法?隨着行數增加,這會開始產生一些非常負面的性能影響嗎? – wgpubs 2010-04-15 08:54:42

+0

好的,另一種方法: 檢查(rowNumber + 1)是否存在於一個winningNumbers數組中並替換cellForRowAtIndexPath方法中的附件。這也應該起作用。 – kovpas 2010-04-15 09:00:06

1

我得到它的工作,通過使用cellForRowAtIndexPath基於跟蹤所選行的變量設置附件類型。然後在didSelectRowAtIndexPath我只是取消選擇該行,更新變量,並重新加載表。

代碼示例:

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    cell.textLabel.text = [NSString stringWithFormat:@"%d", indexPath.row]; 

    if ([_cells indexOfObjectIdenticalTo:_selectedCell] == indexPath.row) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } else { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    [tableView deselectRowAtIndexPath:indexPath animated:NO]; 

    _selectedCell = [_cells objectAtIndex:indexPath.row]; 
    [tableView reloadData]; 
}