I have an UITableView
and when I select a cell I wanna put a checkmark on the right of the cell....which is pretty easy.
To this end I did the following:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableViewMieux deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if (selectedCell.accessoryType == UITableViewCellAccessoryNone)
{
UIImageView *checkBox = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkbox-pressed.png"]] autorelease];
checkBox.frame = CGRectMake(0, 0, 20, 20);
selectedCell.accessoryView = checkBox;
selectedCell.accessoryType=UITableViewCellAccessoryCheckmark;
}
else if (selectedCell.acces开发者_开发百科soryType==UITableViewCellAccessoryCheckmark)
{
UIImageView *checkBox = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkbox.png"]] autorelease];
checkBox.frame = CGRectMake(0, 0, 20, 20);
selectedCell.accessoryView = checkBox;
selectedCell.accessoryType=UITableViewCellAccessoryNone;
}
[tableViewMieux reloadData];
}
Everything works great except the fact that when I select the first cell this gets checkmarked and the fifth cell gets checkmarked automatically too. And so on...if I select the second cell this gets checkmarked ...and another cell from the bottom of the tableView gets marked too.
So, each time I select and mark a cell and extra cell gets marked.
Question:Why?What is the right way to do it?
EDIT:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableViewMieux dequeueReusableCellWithIdentifier:@"CellRecherchePartenaires"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"CellRecherchePartenaires"] autorelease];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.backgroundView.opaque = NO;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.opaque = NO;
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.opaque = NO;
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.highlightedTextColor = [UIColor whiteColor];
cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
cell.accessoryView=UITableViewCellAccessoryNone;
}
// Set up the cell...
[[cell textLabel] setText: [tableArray objectAtIndex:indexPath.row]] ;
return cell;
}
add the following lines
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if([[tableView cellForRowAtIndexPath:indexPath] accessoryType] == UITableViewCellAccessoryCheckmark)
{
temp = [myarray objectAtInadex:indexpath.row];// change the line according to ur code
//temp is any object.. //array is nsmutable array(global)
[array removeObject:temp];
[tableView cellForRowAtIndexPath:indexPath].accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]] autorelease];
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];
return;
}
else
{
//temp is any objecct....//arry is nsmutable array(global)
[array addObject:temp];
[tableView cellForRowAtIndexPath:indexPath].accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"4.png"]] autorelease];
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
}
}
}
In cell for row index path
temp = [myarray objectAtInadex:indexpath.row];// change the line according to ur code
if ([array containsObject:temp]) {
cell.accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"4.png"]] autorelease];
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else
{
cell.accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]] autorelease];
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];
}
In number of Rows In section
[array removeAllObjects];
Try, it will work. sure
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", indexPath.section, indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.backgroundView.opaque = NO;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.opaque = NO;
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.opaque = NO;
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.highlightedTextColor = [UIColor whiteColor];
cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
cell.accessoryView=UITableViewCellAccessoryNone;
}
// Set up the cell...
[[cell textLabel] setText: [tableArray objectAtIndex:indexPath.row]] ;
return cell;
}
Simply try this ...
To the question why that is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
This method that you are calling here: UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
returns a Cell, but apple designed it that way that cells aren't created from scratch every time but rather cells are being reused, so your 5th and 20th cell in a tableview might be the "same one" because when you scroll down, the cell gets released once it's out of vision and the ones you scroll to are being created as you scroll, that way less memory is being used for a tableview.
As to how to solve this issue, I'm not sure but you have to ... apparently look at mrd3650's solution which he posted as I'm typing :)
I also had this problem. It is most probably because you are reusing the cells (with the cell identifier. which is what you should be doing). To solve this, in the delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
do the following:
static NSString * cellIdentifier = @"Cell";
// Get reusable cell if possible
UITableViewCell * cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:cellIdentifier] autorelease];
}
// Remove checkmark if cell is not the one that should be checked
int idx = [workingDatasource indexOfObject:currentSupposedToBeChecked];
if (idx != indexPath.row)
cell.accessoryType = UITableViewCellAccessoryNone;
else
cell.accessoryType = UITableViewCellAccessoryCheckmark;
EDIT: more explanation:
So, workingDatasource is the array which contains the datasource of the UITableView, and currentSupposedToBeChecked is an instance variable containing the actual data (which must be one of the data in the datasource) that should be checked.
So on each touch (i.e. in didSelectRowAtIndexPath:
delegate) you should save the selected object by doing
self.currentSupposedToBeChecked = [workingDatasource objectAtIndex:indexPath.row];
I have added another answer for clarity's sake...
I have made comments //--Added--->
where i have edited.
If there are something which is not clear leave a comment.
Note that self.currentlySelected
must be declared as a NSString
property (can be private)
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableViewMieux deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if (selectedCell.accessoryType == UITableViewCellAccessoryNone)
{
UIImageView *checkBox = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkbox-pressed.png"]] autorelease];
checkBox.frame = CGRectMake(0, 0, 20, 20);
selectedCell.accessoryView = checkBox;
selectedCell.accessoryType=UITableViewCellAccessoryCheckmark;
}
else if (selectedCell.accessoryType==UITableViewCellAccessoryCheckmark)
{
UIImageView *checkBox = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkbox.png"]] autorelease];
checkBox.frame = CGRectMake(0, 0, 20, 20);
selectedCell.accessoryView = checkBox;
selectedCell.accessoryType=UITableViewCellAccessoryNone;
}
//-----ADDED---->>
int idx = [tableArray indexOfObject:self.currentlySelected];
if (idx != indexPath.row)
cell.accessoryType = UITableViewCellAccessoryNone;
else
cell.accessoryType = UITableViewCellAccessoryCheckmark;
//-----ADDED----<<
[tableViewMieux reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableViewMieux dequeueReusableCellWithIdentifier:@"CellRecherchePartenaires"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"CellRecherchePartenaires"] autorelease];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.backgroundView.opaque = NO;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.opaque = NO;
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.opaque = NO;
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.highlightedTextColor = [UIColor whiteColor];
cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
cell.accessoryView=UITableViewCellAccessoryNone;
}
// Set up the cell...
[[cell textLabel] setText: [tableArray objectAtIndex:indexPath.row]] ;
//----ADDED--->>
// Save currently selected text - NOTE this must be a property
self.currentlySelected = [tableArray objectAtIndex:indexPath.row];
//----ADDED---<<
return cell;
}
精彩评论