How to apply checkmark on table view in iphone using object c?

im trying to apply for a checkmark in the table view, but it does not work if I checked again in this cell and then applied the mark. but do not apply in the newly selected cell. anyone who helps me ....

thanks aamir.

im using the following code

#pragma mark - #pragma mark Table Data Source Methods - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger) section { return [self.chaptersList count]; } -(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath { static NSString *CheckMarkCellIdentifier = @"CheckMarkCellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CheckMarkCellIdentifier]; if ( cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CheckMarkCellIdentifier] autorelease]; } NSUInteger row = [indexPath row]; NSUInteger oldRow = [lastIndexPath row]; cell.textLabel.text = [chaptersList objectAtIndex:row]; cell.textLabel.font = [UIFont boldSystemFontOfSize:14]; cell.accessoryType = (row == oldRow && lastIndexPath != nil) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; return cell; } #pragma mark - #pragma mark Table Delegate Methods - (void) tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath { int row = [indexPath row]; int oldRow = [lastIndexPath row]; if (row != oldRow) { UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath]; newCell.accessoryType = UITableViewCellAccessoryCheckmark; UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath]; oldCell.accessoryType = UITableViewCellAccessoryNone; lastIndexPath = indexPath; } [tableView deselectRowAtIndexPath:indexPath animated:YES]; } 
+4
source share
2 answers

Do you mean that the checkbox is not applied when you select the first row for the first time? You can add another sentence to the code, but it takes some time to run the code.

 if (newRow != oldRow) { UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath]; newCell.accessoryType = UITableViewCellAccessoryCheckmark; UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath]; oldCell.accessoryType = UITableViewCellAccessoryNone; lastIndexPath = indexPath; } else { UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath]; newCell.accessoryType = UITableViewCellAccessoryCheckmark; lastIndexPath = indexPath; } 
+8
source

There is a funny mistake here.

  if (row != oldRow) 

because if oldRow was nil (or in other words ... "0" ;-), and the user presses the string = "0", the sentence "if" will consider this string checked.

I recorded the following:

 - (void) tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath { if (!self.lastIndexPath) { self.lastIndexPath = indexPath; } if ([self.lastIndexPath row] != [indexPath row]) { UITableViewCell *newCell = [tableView cellForRowAtIndexPath: indexPath]; newCell.accessoryType = UITableViewCellAccessoryCheckmark; UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:self.lastIndexPath]; oldCell.accessoryType = UITableViewCellAccessoryNone; self.lastIndexPath = indexPath; } else { UITableViewCell *newCell = [tableView cellForRowAtIndexPath: indexPath]; newCell.accessoryType = UITableViewCellAccessoryCheckmark; } [tableView deselectRowAtIndexPath:indexPath animated:YES]; } 

By the way ... don't forget to use the "I"

+2
source

All Articles