a problem when disable Show selection on touch of UITableview? - iphone

I use IB and uncheck the 'Show selection on touch' but it still show blue highlight on cell that is selected. Is this a bug with apple or I am getting something wrong.

This is probably a bug in IB as you see in Documentation that table view does not have any property for the shows Selection on touch. It is the property of tableview cell rather. So the checkbox should not be present in the IB. Probably you can file a bug with apple and see what they say about it.
For getting the effect you should do it like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
}
Hope this helps.

I too feel it's most likely a bug. However, I just chanced upon a perfect workaround based on the following observations.
The cell is highlighted on touch down and selected on touch up.
Both -setHighlighted:animated: and -setSelected:animated: highlight the cell according to its selection style, i.e., turning either of them on while the other is off highlights the cell.
The cell is born with highlighted turned off (typically, else the solution below just needs an appropriate tweak which is easy to figure out based on your specific situation).
Given the above, just subclass UITableViewCell and override setHighlighted:animated: without calling super's implementation. Thus, all efforts to turn highlighted on would be suppressed and the highlighting would happen only on touch up and not on touch down which is exactly what is expected when turning off 'Show Selection on Touch'.
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
// hack to get the effect of unchecking "Show Selection on Touch" option of UITableView in IB which has no effect
}
On touch up, the cell is selected but it is not animated. If you want the animation, I found that calling a delegate method as shown below animates the selection.
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[_itemsTable selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
return indexPath;
}

Related

How to update UITableViewCell drawing

My app displays some news in a UITableView. For each news-topic, the background color of the UITableViewCell is different (added a UIView in my custom cell).
So far, it works. But If I press on a UIBarButton to deselect one news-category, the content changes, but the background stays the same as before.
The color does not change, because the cell does not reload again after button click.
Any ideas?
Thank you!
You can set the color for the cell in (as per your logic):
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
This will make sure the color is set every time the cell is displayed.
In your cellForRowAtIndexPath method, make sure that you're setting the color of the UITableViewCell.
Also, use [tableView reloadData];

How to display the selected cell in highlighted state when i come back in UITableView?

I created a custom cell for my table view and for the most part everything seems to be working fine, but when I select one of the rows (which takes me to another UIView), then come back from the subsequent view via the nav controller, the selected cell is not in highlighted state. How to display the selected cell in highlighted state when i come back?
any help is appreciated in advance, thanks.
when you are coming back from anotherview make sure that save the selectedCell and then in viewwillappear method reloaddata.in cellforindexpath write the code of selection style uitableviewcellselectionstyleblue
As #sachin said, you should save selected index path
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.selectedIndexPath = indexPath;
}
and in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
You should check if indexPath is equal to selectedIndexPath,
but you should be aware that Apple discoureges that kind of behavior in HIG: http://developer.apple.com/library/ios/#DOCUMENTATION/UserExperience/Conceptual/MobileHIG/UIElementGuidelines/UIElementGuidelines.html
In rare cases, a row might remain highlighted when secondary details
or controls related to the row item are displayed in the same screen.
However, this is not encouraged because it is difficult to display
simultaneously a list of choices, a selected item, and related details
or controls without creating an uncomfortably crowded layout.

Add a view to a UITableViewCell's contentView when it is selected

I am trying to add a new view with 5 buttons to a cell once it is selected. I am trying to do this in 3 different ways. The first one is by checking if it is selected inside the
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
method.
if (cell == nil ) {
//initial customizaton of cells
} else {
if ([cell isSelected]) {
//I would add the view to the cell's contentView here but it never enters this if even though the cell is still blue, I scrolled the table back and forth several times and the blue cell never evaluated here
}
}
I have also tried to do it inside the following two methods:
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
And:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
I used tableView:cellForRowAtIndexPath: to get a reference to the cell and when I had that reference I tried adding the view inside the cell's contentView but it didn't seem to work(by "didn't work" I mean it didn't appear, I'll debug this further).
I know I didn't provide much detailed code but that's because I mainly want to know what's the general principle of how it should be done. I want to know why the cell doesn't stay with the selected property equal to YES even though it stays blue (when selectionStyle is the default one) when I tap it. I want it to stay "blue"(selected) with that view inside it until I deselect it and remove said view.
Did you try creating your own custom UITableViewCell? I think that is the point, you can overload setSelected: method there.
I followed NR4TR's advice and so far so good. I have subclassed UITableViewCell and have overridden the setSelected method in it.
Hoping this is useful to someone else in the future!

UItableViewCell - don't allow to select the cell, but can still press buttons inside it?

Can you add a button as a subview to a uitableview cell and have that clickable, but not the cell itself. ie: I don't want the selected view to come up. Obvs I can just not have the cell disclose anywhere, but I don't want it to look "selected" when someone taps it.
Setting user interaction enabled to NO makes the whole thing unenabled, including the buttons.
Any ideas?
Thank you.
Tom
cell.selectionStyle = UITableViewCellSelectionStyleNone;
This will prevent the cell to highlight for a bit before the willSelect returns nil
implement this delegate method:
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
return nil;
}
and set the UITableViewCells selectionStyle property to UITableViewCellSelectionStyleNone
Just select the option "no selection" on the attributes inspector like in the image
Or you could uncheck the user interaction in the Attributes inspector of the cell interface file.
Localice the section: "View" and the subtitle: "Interaction", here you have: "User Interaction Enabled".

Change UITableViewCell appearance when drag handle is pressed

I'd like to change the appearance of a cell when the user tap the drag handle.
I searched in the documentation, forums and google, but I can't find a method or an event that say when the drag icon is pressed.
Any help will be appreciated!
Thanks
Does this function help:
// Allows customization of the target row for a particular row as it is being moved/reordered
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;
It doesn't seem to fire until the cell has been dragged about half way into another cell, though.
It is the only indication I could find that a cell is moving.