Reputation: 604
Hiii, I want to make a tableview and when I tap on cell it will go to next view there I can select a particular option (only one at a time ) with check mark and whatever text in selected option it should be displayed in previous view(which is also a tableview)
Please help me out with this.
While using below method it is selecting all the option which I am tapping,but I wanted to only one cell will have check mark which I have tapped.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
Thanks in advance
Upvotes: 0
Views: 3722
Reputation:
Just use the tableView:didSelectRowAtIndexPath
and tableView:cellForRowAtIndexPath
method together in this way:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
And, if you don't want to make your users see the bad sight of the cell still being highlighted, just add this line of code just before the closing brace:
[tableView deselectRowAtIndexPath:indexPath animated:YES];
P.S. I made that animated just to make it look good.
References:
Half from here: iPhone :UITableView CellAccessory Checkmark
And half from here: How to deselect a selected UITableView cell?
Upvotes: 0
Reputation: 150755
I wrote an example project in Xcode that you might find useful.
The way to do it is to update the datasource, and reload the table. Trying to change the state of the tableview directly isn't going to work.
Upvotes: 2
Reputation: 1484
Let's use A and B to represent your first table view and second table view. And say you can navigate from A to B.
You are going to need these things:
A custom object of what your uitableview cell represented, or NSIndexPath, or even an integer used as index for A. Set the value in A's - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{}, because after you did a selection in B later on, you wanna to know where you will append your text to.
Show check mark in B's - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{}, set it to the currently selected one. You have to figure out a way to record the information, and that should come from your navigation process from A to B. What I usually do is create a custom init function of B, call from A. You can also make a current selected object property in B, and set it up from A when you init B before navigate from A. No matter what you do, you probably want to create that property anyway.
Make A the delegate of your B, implement a function -(void)getSelectionFromB:(SomeClass *)obj{} and inside B's - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{}, call [delegate getSelectionFromB:selectedObject], and you should be alright.
In A's implementation of -(void)getSelectionFromB:(SomeClass *)obj{}, either find the cell in the tableview and change it, or change the NSArray or whatever you use to populate your table view, and then reload the data.
Hope this helps.
Upvotes: 0
Reputation: 5157
You could track the currently selected item in a class property of type NSIndexPath
. First of all initialize this property to nil
or whatever value was previously selected. You could also fix up your cellForRowAtIndexPath
to compare the cell's indexPath with the "currently selected" index path property and add a checkmark there if they match. Now all you have to do in your didSelectRowAtIndexPath
is something like this (code not checked with compiler, might conatin small errors):
NSIndexPath *oldIndexPath = self.selectedIndexPath; // retain this if not using ARC!
self.selectedIndexPath = indexPath; // passed in from didSelectRowAtIndexPath
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects: oldIndexPath, self.selectedIndexPath, nil], UITableViewRowAnimationNone];
// release oldIndexpath if noct using ARC
As cellForRowAtIndexPath
should create the cell with or without the checkmark depending on self.selectedIndexPath
this will update the cells accordingly (and also create the correct view in the beginning if something should already be selected).
Bonus: you have self.selectedIndexPath
available to determine what you should return to the "calling" view, i.e. which row is selected.
Upvotes: 0