Objective-C: Set another UITableViewCell on select - ios

I have two custom models of TableViewCell, one for just basic informations and another for detailed informations.
How can I switch to the detailed one with the didSelectRowAtIndexPath method? (and afterwards if the detailed one is displayed, toggle to the basic one on select)
Thank you.

When the row is selected, toggle a flag indicating that the row needs to switch from one type of cell to another. Then reload the the cell at that index path.
Then your cellForRowAtIndexPath method looks at the flag for the given index path and returns one of the two types of cells.

In whatever kind of object you use to represent cell data, keep a flag for whether it's selected or not. Turn the flag on or off in didSelectRowAtIndexPath and then reload the table view data. When you return a cell for an index path, choose which kind of cell based on the flag value.

You can create an array or set consist of indexPaths of cells type 1. Add or remove from it cells if needed. Return the right cell in
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
And if you want to reload specific cell, modify your array of cells and just call from tableView:
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths
withRowAnimation:(UITableViewRowAnimation)animation

Related

Handle deselect row in table view

I want to handle case when user deselect row (that already selected) by tap on this row. It is well known that tableView:willDeselectRowAtIndexPath: / tableView:didDeselectRowAtIndexPath: delegate methods not called in this case: they are called only if you tap on other, unselected yet row (my table view have single selection mode).
tableView:didSelectRowAtIndexPath: method also not called when I am deselecting row.
Is there is any easy solutions?
Update
The problem was in tableView:willSelectRowAtIndexPath: method, where I return nil in some cases, thats why tableView:didSelectRowAtIndexPath: didn't call. Thanks #Nekto for useful information and helping.
Apple docs say next about - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
This method isn’t called when the editing property of the table is set to YES (that is, the table view is in editing mode).
That means that this method should be called unless your code has a bug or you haven't updated delegate of your table view or table is in editing mode.
One of the possible problems could be that you have incorrectly implemented - (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath and are returning nil for the selected row. In that case didSelect... isn't called:
Return nil if you don’t want the row deselected.
Alternatively you can implement another table view delegate method: - (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath:
Tells the delegate that the highlight was removed from the row at the specified index path.
See more about managing selections in table views here.
You can still use tableView:didSelectRowAtIndexPath, just use it like a check mark system would work.
The answer here explains how to use a checkmark. You can use this, or use the answer here.
I have such use case. Why you don't create a variable for lastSelectedRow(here I have problem if it was NSIndexPath and I used two integers) and on the method for selecting row just compare currentSelectedRow with lastSelectedRow.

One click, multiple actions Custom Cell of TableView

What I do
I have a created a custom cell which has a button with a specific image on it. Now depending on some conditions I set that image image1.jpg or image2.jpg.
How I do that
The custom cell is created by creating a protocol where I click that buttons action, I notice in the main interface, that the button is pressed and I pass as a parameter the button instance.
Issue
Now, the problem is when I click that button in a specific cell, I change cell.button's image, but also with that, when I scroll down, I see that there are other cells that have changed their image.
Any idea why is this happening?
Due to the reuse of cells by the UITableView, when the cell moves out of the screen, it is moved to a queue and then reused when there are new cells to load.
If you want to keep each cell's property unique, you will have to reconstruct its values every time. For that you can keep some kind of data array that stores the state/condition of each cell. The array will be ordered by the indexPath of the tableView, and then you can just fetch the state from the array based on the indexPath and update the cell to be the right image.
What is happening is that when you change the image of your cell and later scroll down or up, your cell is being reused to present another information.
To fix that you should check your UITableView data source, see if you're reusing cells in this method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
I'd recommend to configure the cell before returning it in the previous method, and assign the appropiate image for the button at that time. To achieve that you'd need some mechanism for remembering the state of each cell.
For more information about cell reusing and cell in general you could go to the Apple Docs for UITableViewCell
As described in above answer you can manage button selected and unselected state in array for that you can even add one key in the array that you are using for the data to display.
and on didselect event of table you can change the key of selected field like this
NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];
NSDictionary *oldDict = self.dict_trans_Details ;
[newDict addEntriesFromDictionary:oldDict];
[newDict setObject:status forKey:#"transactionstatusid"];
[table_transdetails_array replaceObjectAtIndex:self.indexValue withObject:newDict];
Thanks.

deselectRowAtIndexPath - Was it needed?

I was following this tutorial: https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOS/ThirdTutorial.html#//apple_ref/doc/uid/TP40011343-CH10-SW1,
where you have a table view.
The table view has Dynamic prototype cells.
During the tutorial, I selected a cell on storyboard, and set the 'Selection' type to: none.
But in the final part of the tutorial, still we were told to add
[tableView deselectRowAtIndexPath:indexPath animated:NO];
line inside the
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
method.
For me this seemed redundant, due to the 'Selection´ is none which I did as above.
Or I am wrong? Since I selected none as Selection in storyboard(for a cell), why do we need that deselect method? The cell won't be higlighted anyway, isn't it?
The row is still selected. The style of 'none' only relates to how it looks when selected.
So yes, you need to deselect the row when you are done with it.
Changing the "Selection" setting in storyboard changes the way that the cell looks when selected. (Thank you rmaddy, that was my mistake)
The line in didSelectRowAtIndexPath will perform some operation when a specific row is tapped, and then removes the selected graphic from the cell that was just tapped.

How to identify a selected static UITableViewCell

I've thought of a couple of approaches to use inside the - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method:
Create outlets and link the cells to them, then check which one was selected by comparing the reference
Set and check a unique reuseIdentifier property on each cell
Use the indexPath, because you know which cell is at which index since they're defined statically anyway
What is the best approach? Or perhaps there's another, better approach?
Definitely indexpath is the best approach as many of the delegates of tableview works on indexpaths. You can easily get hold of the rows by keeping a track of indexpath. I guess you can also put to use the property of cell called 'selected' to check whether a cell state is selected or not. Hope this helps. Also make use of apple docs on tableviews.

Grouped TableView selection problem

Ok, I have a grouped TableView that has the following overridden method:
-(NSIndexPath*)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
return nil;
}
... Obvious enough, to disable selection.
BUT!
If the user presses and holds on a cell, it gets highlighted (selected)!! I need to disable this, too.
On a side note, I am using the tableView to display static Data, almost like the About tableView in Settings > General. It just loads the info from an array of strings that I created manually.
If there is a better way to represent the data, please do tell!
Thanks!
Set the selectionStyle property of the cell to UITableViewCellSelectionStyleNone.
See the documentation for tableView:willSelectRowAtIndexPath: to see why this is needed even though you're indicating that you don't want the cell selected.
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
...did the trick.

Resources