I have a UITableView that displays checkmarks when a row is selected. The problem is that When i select a row in didSelectRowAtIndexPath and add a checkmark on the selected row it adds an additional checkmark. Here's my code
Any help would be very much appreciated.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text=[[Model.category objectAtIndex:indexPath.row] categoryName];
cell.imageView.image=[[Model.category objectAtIndex:indexPath.row]categoryImage];
//cell.detailTextLabel.text =#"Breve Descripción de la categoria";
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([self.tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark) {
[self.tableView cellForRowAtIndexPath:indexPath].accessoryType =UITableViewCellAccessoryNone;
[self.cellSelected removeObject:indexPath];
}else {
[tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryCheckmark;
[self.cellSelected addObject:indexPath];
}
[self checkMark];
[tableView reloadData];
}
- (void)checkMark{
for (NSIndexPath * indexPath in self.cellSelected) {
[self.tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryCheckmark;
}
}
[self.tableView cellForRowAtIndexPath:indexPath] call in the didSelectRowAtIndexPath will not return the exact cell. It can be same cell, new cell or reused cell. If it is a reused cell at its accessory view has a checkmark, you will end up having two cells with checkmark.
Its better to store in the array and use it accordingly. If you are planning to have multiple selections, Use the code example below.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.cellSelected = [NSMutableArray array];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Cell Initialisation here
if ([self.cellSelected containsObject:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//if you want only one cell to be selected use a local NSIndexPath property instead of array. and use the code below
//self.selectedIndexPath = indexPath;
//the below code will allow multiple selection
if ([self.cellSelected containsObject:indexPath])
{
[self.cellSelected removeObject:indexPath];
}
else
{
[self.cellSelected addObject:indexPath];
}
[tableView reloadData];
}
Try this:
Declare in your .m file:
#interface MyViewController () {
NSIndexPath *__selectedPath;
}
In tableView:cellForRowAtIndexPath: check if given indexPath is the same as stored in ivar:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//configure cell
if ([__selectedPath isEqual:indexPath]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
In tableView:didSelectRowAtIndexPath store pointer to selected NSIndexPath or nil if cell has been deselect
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
__selectedPath = indexPath;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
__selectedPath = nil;
}
}
I wrote it without checking in Xcode, so there could be some typos, but I show main idea.
In my opinion you shouldn't call [self checkMark]; in your tableView:cellForRowAtIndexPath: method.
Moreover if you want to have only one selected cell at a time, you should't create NSMutableArray to store NSIndexPath. It seems yours cellSelected stores 2 NSIndexPaths at a time, that why you have this strange behaviour.
Related
Even though it's common question but I'm not getting how to fix this.
I referred Selecting one UITableViewCell selects the other cells at that same position when you scroll up or down But i didn't understand.
I'm having allMumbaiArray (using in cellForRowAtIndexPath) and tableview alllMumbaiTableview. By using following code i can select and deselect the row.
UPDATED as per EI Captain's solution
#property (nonatomic, strong) NSMutableArray *arrIndexpaths;
tableView Methods -:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"CellIdentifier"];
}
cell.textLabel.text = [allMumbaiArray objectAtIndex:indexPath.row];
if ([self.arrIndexpaths containsObject:[NSNumber numberWithInt:indexPath.row]])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.view endEditing:YES];
NSString *testing =[allMumbaiArray objectAtIndex:indexPath.row];
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.arrIndexpaths addObject:[NSNumber numberWithInt:indexPath.row]];
NSLog(#"%#",testing);
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
tableViewCell.accessoryType = UITableViewCellAccessoryNone;
[self.arrIndexpaths removeObject:[NSNumber numberWithInt:indexPath.row]];
}
Issue : 1) when i select first row after scrolling the tablewview another row get selected.How to Fix this?
2) After selecting and deselecting row continuously, how can i maintain the only selected row values ?
Here is the solution
make one mutable array that stores selected index of tableview cell..
#property (nonatomic, strong) NSMutableArray *arrIndexpaths;
cellForRowAtIndexPath
if ([self.arrIndexpaths containsObject:[NSNumber numberWithLong: indexPath.row]])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
tableViewCell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.arrIndexpaths addObject:[NSIndexSet indexSetWithIndex:indexPath.row]];
}
didDeselectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
tableViewCell.accessoryType = UITableViewCellAccessoryNone;
[self.arrIndexpaths removeObject:[NSIndexSet indexSetWithIndex:indexPath.row]];
}
Here is the sample project...
demo project
If your cell are not showing accessory then you just need to reload them
[_ableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone];
try this in didSelectRowAtIndexPath and didDeselectRowAtIndexPath.
I have a UITableView that holds a tableView that populates 'prototype cells' from a mutable array and whose cells show an 'accessory checkmark' when you select them. I have a textfield in a view below the tableview and whose data I am then appending to the array that is populating the tableview. My problem is that after I append the new data which adds a cell to the tableview I have to touch a cell twice in order to deselect any of the cells I previously selected.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"skillName" forIndexPath:indexPath];
cell.textLabel.text = skillsOptions[indexPath.row];
// Configure the cell...
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
//tableViewCell.accessoryView.hidden = NO;
tableViewCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
//tableViewCell.accessoryView.hidden = YES;
tableViewCell.accessoryType = UITableViewCellAccessoryNone;
[skillsList removeObject:skillsOptions[indexPath.row]];
}
-(void)grabSelectedSkills {
for (NSIndexPath *indexPath in self.tableView.indexPathsForSelectedRows) {
NSString *skill = skillsOptions[indexPath.row];
//NSString *skill = [NSString stringWithFormat:#"%li",(long)indexPath.row];
[skillsList addObject:skill];
}
NSLog(#"skillsList: %#",skillsList);
}
- (IBAction)continue:(id)sender {
[self grabSelectedSkills];
}
- (IBAction)addOtherSkills:(id)sender {
if (self.otherSkill.text.length > 1) {
[skillsOptions addObject:self.otherSkill.text];
self.otherSkill.text = #"";
[self.tableView endEditing:YES];
[self.tableView reloadData];
}
You need to track your selection/deselection status outside of the cell - a dictionary keyed by the "skill option" is probably a good choice.
You can then use this in cellForRowAtIndexPath and didSelectRowAtIndexPath to add/remove the check mark as required. You should deselect the row in didSelectRowAtIndexPath and get rid of didDeselectRowAtIndexPath -
Create self.selectedCells as an NSMutableDictionary property.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"skillName" forIndexPath:indexPath];
// Configure the cell...
NSString *skillOption=skillsOptions[indexPath.row];
cell.textLabel.text = skillOption;
if (self.selectedCells[skillOption]!= nil) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *skillOption=skillsOptions[indexPath.row];
if (self.selectedCells[skillOption]!= nil) {
tableViewCell.accessoryType = UITableViewCellAccessoryNone;
[self.selectedCells removeObjectForKey:skillOption];
} else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.selectedCells[skillOption]=skillOption;
}
[tableview deselectRowAtIndexPath:indexPath];
}
I think you should only use didSelectRowAtIndexPath: method (drop the didDeselectRowAtIndexPath: method) and check to select the cell if it's not and vice versa as-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BOOL cellSelected = NO;
// set cellSelected based on your condition
if(cellSelected)
{
// code to deselect the cell
}
else
{
// code to select the cell
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
//tableViewCell.accessoryView.hidden = NO;
tableViewCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
How do i store all selected cells text label text in a NSMutableArray?
How do i remove the correct cells text from the NSMutableArray when a cell is deselected?
What i have this far:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = #"Cell";
PFTableViewCell *cell = (PFTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[PFTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if ([self.allSelectedUsers containsObject:indexPath]) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
NSLog(#"Yeeees");
}else{
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
// Configure the cell
cell.textLabel.text = object[#"username"];
return cell;
}
This is when i'm selecting a cell:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([self.allSelectedUsers containsObject:indexPath]) {
[self.allSelectedUsers removeObject:indexPath];
}
else{
[self.allSelectedUsers addObject:indexPath];
}
NSLog(#"%d", self.allSelectedUsers);
[tableView reloadData];
}
When i check the log it doesn't display anything about the cells text label.
As I can't see how you're getting object instance, I suggest you to get back cell and read the title again.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Lazy initialise array
if (!self.allSelectedUsers) {
self.allSelectedUsers = [NSMutableArray new];
}
// Take action
if ([self.allSelectedUsers containsObject:indexPath]) {
[self.allSelectedUsers removeObject:indexPath];
} else {
[self.allSelectedUsers addObject:indexPath];
}
[tableView reloadData];
// Logging all selected users
for (NSIndexPath *sIndexPath in self.allSelectedUsers) {
NSLog(#"%#", [tableView cellForRowAtIndexPath:sIndexPath].textLabel.text);
}
}
You are currently storing NSIndexPath objects, not NSString objects, so it's not exactly what your question is asking. With your PFTableViewController, you have access to the selector objectAtIndexPath:.
for (NSIndexPath *indexPath in self.allSelectedUsers) {
NSLog(#"%#", [self objectAtIndexPath:indexPath][#"username"]);
}
Note: You shouldn't be calling reloadData in your didSelectRowAtIndexPath: responder; change the accessory type for the cell instead.
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
You should also implement didDeselectRowAtIndexPath: to know when a user deselects a row.
I am trying to implement a selectable list view. The list view contains some names. You can select or deselect multiple names. If a name is selected the cell style is changed to UITableViewCellAccessoryCheckmark.
But now I have a problem. Before you can deselect a name you have to press multiple times onto the cell. This is because didSelectRowAtIndexPath gets always called first.
// cellForRowAtIndexPath function
if ([assignedPlayers containsObject:player.persistentData])
{
[cell setSelected:true];
[cell setAccessoryType: UITableViewCellAccessoryCheckmark];
}
cell.textLabel.text = [NSString stringWithFormat:#"%#", player.persistentData.name];
return cell;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
[[tableView cellForRowAtIndexPath:indexPath] setSelected:FALSE];
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType: UITableViewCellAccessoryNone];
[self.eventController removePlayerFromEvent:_editingEvent :[self.playerController getPlayerAtPosition:indexPath.row]];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[[tableView cellForRowAtIndexPath:indexPath] setSelected:TRUE];
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType: UITableViewCellAccessoryCheckmark];
[self.eventController addPlayerToEvent:_editingEvent :[self.playerController getPlayerAtPosition:indexPath.row]];
}
Now I don't know how to fix this.
thanks for your help.
Have a look at the example below where _selectedList contains the selected players and _datasoureArray is the datasource that contains all the players names.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CELL"];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"CELL"];
}
cell.textLabel.text = [_datasoureArray objectAtIndex:indexPath.row];
if([_selectedList containsObject:[_datasoureArray objectAtIndex:indexPath.row]]) //hear selected list is an mutable array wich holds only selected player
{
cell.selected = YES;
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.selected = NO;
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell)
{
if(cell.accessoryType == UITableViewCellAccessoryNone)
{
[_selectedList addObject:[_datasoureArray objectAtIndex:indexPath.row]];//add
}
else
{
if([_selectedList containsObject:[_datasoureArray objectAtIndex:indexPath.row]])
{
[_selectedList removeObject:[_datasoureArray objectAtIndex:indexPath.row]]; //remove
}
}
}
[tableView reloadData];
NSLog(#"%#",_selectedList.description);
}
You need to make the tableView to support multiple selection. Try the following code in viewDidLoad:
self.myTableView.allowsMultipleSelection = YES;
Then you won't need these two lines in the respective delegates:
[[tableView cellForRowAtIndexPath:indexPath] setSelected:TRUE];
[[tableView cellForRowAtIndexPath:indexPath] setSelected:FALSE];
Maintain a check for each selected indexes for e.g.: use an as Selected Indexes
No every time you select a cell add it to this array such as:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([selectedIndex containsObject:indexPath]) {
[selectedIndexes removeObject:indexPath];
}else{
[selectedIndexes addObject:indexPath];
}
[tableView reloadData];
}
and then in CellForROwAtIndex
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([selectedIndex containsObject:indexPath]) {
[cell setSelected:TRUE];
[cell setAccessoryType: UITableViewCellAccessoryCheckmark];
[self.eventController addPlayerToEvent:_editingEvent :[self.playerController getPlayerAtPosition:indexPath.row]];
}
}else{
[cell setSelected:FALSE];
[cell setAccessoryType: UITableViewCellAccessoryNone];
[self.eventController removePlayerFromEvent:_editingEvent :[self.playerController getPlayerAtPosition:indexPath.row]];
}
}
this would be more effective rather then fetching the Cell from DidSelect Method just make sure to use [tableView reloadData]; in didSelectRowAtIndexPath
You should set the accessoryType to UITableViewCellAccessoryNone when not containsObject.
Because the cell could be reused.
And you don't need to set selected when select or disselect.
if ([assignedPlayers containsObject:player.persistentData]){
[cell setAccessoryType: UITableViewCellAccessoryCheckmark];
} else {
cell.accessoryType= UITableViewCellAccessoryNone;
}
You can implement functionality in following way,
When you select a row you will check your array for person corresponding to that row if it already persent that remove it from array else add it to the array and reload your table. And in cellForRowAtIndexPath method check array if person exist set selected true else set selected false.
// cellForRowAtIndexPath function
{
if ([assignedPlayers containsObject:player.persistentData])
{
[cell setSelected:true];
[cell setAccessoryType: UITableViewCellAccessoryCheckmark];
}
else
{
[cell setSelected:Flase];
[cell setAccessoryType: UITableViewCellAccessoryCheckmarkNone];
}
cell.textLabel.text = [NSString stringWithFormat:#"%#", player.persistentData.name];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([assignedPlayers containsObject:player.persistentData])
{
[assignedPlayers removeObject:player.persistentData];
}
else
{
[assignedPlayers addObject:player.persistentData];
}
[tableView reloadData];
}
In my UITableView called _selectAttributes I want to put and remove a checkmark when I tap on each cell.
This is the code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell = [_selectAttributes cellForRowAtIndexPath:indexPath];
if (cell.accessoryType != UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
Everything seems to work well, but when I scroll up and down the table, checkmarks appear on other cells and disappear on the previous ones.
How can I solve?
Thank you in advance.
Declare an NSIndexPath property named selectedIndexPath.
Then have your delegate methods cellForRowAtIndexPath and didSelectRowAtIndexPath like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
if ([indexPath isEqual:self.selectedIndexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:self.selectedIndexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
self.selectedIndexPath = indexPath;
}
UPDATE:
I wasn't paying attention that you want solution for multiple cell selection. My answer obviously solves problem only for single cell selection, but I believe its a good start.
I would create an NSArray of selected index paths. On tableView:didSelectRowAtIndexPath:, add the index path to that array and in tableView:cellForRowAtIndexPath: check whether the index path is in the array and set the UITableViewCell's accessory type accordingly.