A table that allows only one cell to be checkmarked.
Hey guys,
Does anyone know how to have a table where you can only put a checkmark on one cell. Like, if I choose one cell, it deselects the other one? Kind of like the ringtones part with the iphone.
Keep an instance variable that tracks the currently selected row. Then in the 'didSelectRowAtIndexPath' table delegate method you remove the checkmark from the row that currently has the checkmark, update the instance variable with the new row, and add the checkmark to the newly selected row.
Basically what you need to do is keep track of the currently selected object in your table. Whenever someone selects a row at any index path, you’ll need to see if it matches your current object (do nothing), or if it’s a different object. If it’s different, you’ll need to get the index of the currently selected object, deselect it, and then check the one that’s touched.
If you run into issues, post again and I’ll share what code I used. I’m pretty sure I was able to find some example code from Apple on how to do this, but I don’t remember what project it was.
Basically what you need to do is keep track of the currently selected object in your table. Whenever someone selects a row at any index path, you’ll need to see if it matches your current object (do nothing), or if it’s a different object. If it’s different, you’ll need to get the index of the currently selected object, deselect it, and then check the one that’s touched.
If you run into issues, post again and I’ll share what code I used. I’m pretty sure I was able to find some example code from Apple on how to do this, but I don’t remember what project it was.
[Looks like RickMaddy beat me to the punch.]
Thanks for the prompt reply guys. The problem that I have now is finding the cell that has the check mark on it. I don't know how to iterate through my cells and check if cell.accessoryType == UITableViewCellAccessoryCheckMark. I know how to place a check mark but I don't know how to DECHECK the previous one.
It's much easier than that. Keep an instance variable of type 'int' representing the last selected row.
Now when the user selects a new row you create a new IndexPath object with old row number from your instance variable and the appropriate section (probably 0). You now use that IndexPath to get the cell from the table. Clear that cell's accessory view.
Now repeat with the IndexPath of the newly selected row and set its accessory view to the checkbox and then update your instance variable with the new row value.
It's much easier than that. Keep an instance variable of type 'int' representing the last selected row.
Now when the user selects a new row you create a new IndexPath object with old row number from your instance variable and the appropriate section (probably 0). You now use that IndexPath to get the cell from the table. Clear that cell's accessory view.
Now repeat with the IndexPath of the newly selected row and set its accessory view to the checkbox and then update your instance variable with the new row value.
Ok, thanks I got it to work by creating an indexPath variable. However, what I would like to know is how exactly indexPaths work. I know that you can do indexpath.row or indexpath.section to get what row and section. But how do you go about creating an indexpath from a row num and row section? Btw, your help is really appreciated. thanks
Read the docs on NSIndexPath - but look at the right docs. There is the NSIndexPath class and the NSIndexPath(UITableView) category. You want the latter. There is a class level method for creating an index path used for tables - it takes two arguments - row and section.
Just a note: I took a little bit of a different approach and don’t track the index row of the selected cell. Instead, I keep track of the object (which is a lot more useful in how I’m using this idea) that’s selected and look at the (IIRC) indexOfObject and use that in order to select the row that I want to deselect. I don’t think this would work with multiple sections though.
Read the docs on NSIndexPath - but look at the right docs. There is the NSIndexPath class and the NSIndexPath(UITableView) category. You want the latter. There is a class level method for creating an index path used for tables - it takes two arguments - row and section.
Hmm. Is it in the header files? In the documentation the closest I can find is:
- (NSIndexPath *)indexPathForCellUITableViewCell *)cell
Hi All,
I found this thread useful.. so, thought I may get solution for my problem..
I was just wondering if there is any way to select multiple rows and once multiple rows are selected do some action.
For example, selecting multiple mails from 'inbox' and deleting in one shot.
To implement this, what we have to use from iPhone SDK?
In your data model keep track of the currently selected cell, or in each model object keep track of whether the object is selected or not. Tell the model object that held the old selection to be not selected. Don't try to unselect a row. Instead, reloadData.
The cell you're trying to unselect may not even exist. The idea of iterating through all the cells in the table doesn't match how UITableView works.
Hmm. Is it in the header files? In the documentation the closest I can find is:
- (NSIndexPath *)indexPathForCellUITableViewCell *)cell
I'm looking at the iPhone docs right in Xcode. If I type in NSIndexPath in the documentation search it shows two entries. A class and a category named NSIndexPath. If I click on the category NSIndexPath I can see the class method for creating a table specific index path.
The method you are quoting is in the UITableView class - wrong place.
Basically what you need to do is keep track of the currently selected object in your table. Whenever someone selects a row at any index path, you’ll need to see if it matches your current object (do nothing), or if it’s a different object. If it’s different, you’ll need to get the index of the currently selected object, deselect it, and then check the one that’s touched.
If you run into issues, post again and I’ll share what code I used. I’m pretty sure I was able to find some example code from Apple on how to do this, but I don’t remember what project it was.
[Looks like RickMaddy beat me to the punch.]
hi, I have just found this thread which is what I am looking for, I have the same problem like noobAppDeveloper encountered. I have been searching for any helpful solution for several days, finally I saw a light in the tunnel. But I still don't know how to get the previous accessoryType to decheckmark old row and checkmark new row, please could you post some code here ? bunch thanks!
hi Brian, thanks for your reply, I've seen this many times but what is the function of "setSelection" ? can you post the setSelection code here for reference please?
If the current item is in the array, show the checkmark.
If a checked item is selected again, remove it from the array.
First off, thanks for such a quick response!
Secondly, thanks you for clearing up what was actually going on with the comparison. I don't know why I didn't grasp that earlier, but once you pointed out that illuminated that one of the arrays is empty, it clicked. Thanks.
Here's what I have now, but for some reason the checkmark isn't appearing. According to the NSLog, the action is happening, just no checkmark. Also, getting a warning that "'NSMutable Array' may not respond to '-localizedCase...Compare'.