Problem implementing a UITableView that allows for multiple row selections
This
- (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];
}
... modifies the "accessoryType" of every 6th cell INSTEAD of just the selected row. What am I missing?
Thanks
UPDATED: Here is the cell creation code ...
- (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;
}
MY SOLUTION base开发者_如何学JAVAd on the marked answer below is ...
- (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;
}
If there is a better way I'm all ears. Would be nice if we could just change the SPECIFIC Cell according to the NSIndexPath passed in someday (at least that seems a whole lot more intuitive to me).
I got it to work by using setting the accessory type in cellForRowAtIndexPath based on variable which keeps track of the selected row. Then in didSelectRowAtIndexPath i just deselect the row, update the variable, and reload the table.
CODE SAMPLE:
- (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];
}
Yeah, just figured out. These are the same cells ( logs below are 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 uses already created cell, if the previous one is out of a screen.
So, you should avoid using "dequeueReusableCellWithIdentifier" - create a dictionary NSIndexPath -> UITableViewCell and use it instead of dequeueReusableCellWithIdentifier.
ADDED: You can also check if the (rowNumber+1) exists in a winningNumbers array and replace accessory in a tableView:cellForRowAtIndexPath: method. This should work too
精彩评论