How to install accessory type when cell is selected

I am trying to figure out how to set the accessoriesType attribute in a UITableViewCellAccessoryCheckmark when a cell is selected, but it's hard for me to find a decent example of this.

If you know how to do this or a good tutorial, you can tell me that it would be great.

+4
source share
5 answers

To limit the user to only one choice, which means creating an exclusive list of only one choice, you can follow these steps;

First, you have a global index path specified in your .h file to track an already selected cell ->

NSIndexPath *oldIndexPath; 

When you create cells, do not forget to set the accessory type to none, so that no cell is selected by default when viewing the table;

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CallIdentifier"]; cell.accessoryType = UITableViewCellAccessoryNone; } return cell; } 

Finally, in the didSelectRowAtIndexPath delegate method, add the following code, which will remove the checkmark from the already selected cell and add the checkmark to the newly selected.

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if (oldIndexPath==nil) { // No selection made yet oldIndexPath=indexPath; [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; } else { UITableViewCell *formerSelectedcell = [tableView cellForRowAtIndexPath:oldIndexPath]; // finding the already selected cell [formerSelectedcell setAccessoryType:UITableViewCellAccessoryNone]; [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; // 'select' the new cell oldIndexPath=indexPath; } } 

Hope this works! :)

+10
source

Something like this might work:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [self tableView:myTableView cellForRowAtIndexPath:indexPath]; [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; } 

To respond to the comment below, just click viewController in the same way as this:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [self tableView:myTableView cellForRowAtIndexPath:indexPath]; [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; // Then push a new view iPhoneCustomViewController *myVC = [[iPhoneCustomViewController alloc] initWithNibName:@"iPhoneCustomViewController" bundle:nil]; [self.navigationController pushViewController:myVC animated:YES]; [myVC release]; // And deselect the row (if desired) [tableView deselectRowAtIndexPath:indexPath animated:YES]; } 
+2
source

Do you know that:

1.) Does a UITableView track pointer paths for selected rows? It is in an array called indexPathsForSelectedRows

2.) The UITableView has a flag that you can set for its single or multiple selection. You can change it by calling setter setAllowsMultipleSelection: (BOOL).

So, assuming the table has been configured with one choice, we can do the following in the tableView method: CellForRowAtIndexPath ...

 -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *simpleTableIdentifier = @"CellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; } [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; [[cell textLabel] setText:@"Some Text"]; NSArray *selectedIndexPaths = [tableView indexPathsForSelectedRows]; if ([selectedIndexPaths containsObject:indexPath]) { [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; }else{ [cell setAccessoryType:UITableViewCellAccessoryNone]; } return cell;} 

This implementation of CellForRowAtIndexPath will give you a clean checkmark without a gray background when selecting a cell. You will need to check the didSelectRowAtIndexPath delegation method to ensure that the cell receives the checkmark when it is selected.

No need to create separate ivars or anything else to keep track of what was or was not selected. All of this is neatly contained in a UITableView, as Apple intended.

+1
source
 UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath]; newCell.accessoryType=UITableViewCellAccessoryCheckmark; 

Embed this in the didSelectRowAtIndexPath delegation method

0
source

From docs :

The delegate processes the selection in this method. One of them can only assign a checkmark image (UITableViewCellAccessoryCheckmark) to one line in a section (radio list style). This method is not called when the editing property for the table is set to YES (that is, the view of the table is in edit mode). See “Election Management” in the “Table Programming Guide” for iOS for more information (and code examples) related to this Method.

Here is an example:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath] cell.UITableViewAccessoryType = UITableViewCellAccessoryCheckmark; } 
0
source

All Articles