UITableViewCell selectedBackgroundView customization - ios

I have a custom UITableViewCell with a custom selectedBackgroundView. Is there a delegate method when the selectedBackgroundView shows? For example, if I press one cell and keep it pressed, the cell is highlighted and the selectedBackgroundView is showing. I want to change one of the cell's subviews when this happens.
I have tried using the gesture recognizer's delegate method -(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer, but this gets called too late. I want a to know the second it starts that selection animation. If this can't be done, is there a way to make the selectedBackgroundView be on top of all the cell's subviews?

Figured it out. You have to override -(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated and perform the changes there. I also tried overriding the touchesBegan event, but doing this completely removes the cell's selection (doesn't get selected anymore).

Related

Disabling touch interaction of Spaces between Custom table view cell

I use custom cells of subclassing UITableViewCell in a table view. And There must be space between cells. I succesfully add the space with adding a subview that I called SpaceView into the content view. But When I touched the spaceView, it is perceived as I touched the cell. And didSelectRow method called. I tried to set spaceView's UserInteractionEnabled==NO. But It doesn't work.
So my question is; Is this the right way I used to add space between cells ? Should I try to add cells for making the space ? or If its the right way, How can I prevent calling "didSelectRowAtIndexPath" method when I touched spaceView ?
like Inder said below, u can use an UIButton to solve the issue.
1) in your custom cell: at the bottom of your custom cell add a blank UIButton with the height u actually need between cells, and customize its background color according to your needs.
2) in cellforrowatindexpath: disable button of each cell. (or you can also do that in Interface Builder of previous step)
result: u have a clear disabled button that will appear as required space between cells.
Make your spaceView as UIButton (I guess it's UIView right now)
When you add UIButton that touch will be consumed by the button thus touch won't be passed to it's parent.
OR
I'm not sure if it will work or not, you can add tap gesture to your spaceView

UITableView : click in blank area will not trigger didSelectRowAtIndexPath

When I am developing iOS8 Today Extension, I set a UITableViewController as the
main view controller.And I met this problem:
When I init the UITableViewCell using initWithStyle:UITableViewCellStyleDefault
in cellForRowAtIndexPath, the whole area of one cell can trigger didSelectRowAtIndexPath
normally. However when set UITableViewCellStyle to UITableViewCellStyleValue1,
UITableViewCellStyleValue2 or UITableViewCellStyleSubtitle, I can only trigger
didSelectRowAtIndexPath by click in detailTextLabel or imageView's area. Click in other blank
areas will get no respond.
I've tried to set tableview's delegate to self, set detailTextLabel and imageView's
setUserInteractionEnabled:false in cellForRowAtIndexPath, and all these do not work. Does
anyone have any idea about this?
P.S. I am not using storyboard or xib file,just code.
I've found a same problem on Android:
I'd like to click anywhere (blank area) in a list view
I encountered this myself while trying to display UITableView inside the TodayView. Although I don't know why is this happening, I found out that this only occurs when the UITableViewCell background is clearColor. If you set the background to some other color, didSelectRowAtIndexPath will be called. However, here's a quick workaround - put UILabel to stretch on whole area of the cell's contentView and then the delegate method will be called every time. Anyway, it seems like a bug.

How do I get selectRowAtIndexPath: to work with willDisplayCell:?

First off I have a custom tableview subclass that uses a uiview subclass with drawRect: to handle the cell's content view. Now since I need a the cell's selected to be a certain color I set the cell's selectedBackgroundView in willDisplayCell:
This works great until I need to scroll to and highlight/select (but not call didSelectCell) upon showing the tableview. When I try to call selectRowAtIndexPath: the tableview scrolls to the cell but the selected color is only shown in the cell where the contentView isn't (by the disclosure arrow).
Now I believe this is because selectRowAtIndexPath: highlights the cell then scrolls to it. And willDisplayCell needs to be invoked before the cell is highlighted. However willDisplayCell is called "only when animating rows as they slide in or out" i.e. after the highlight and before the scroll.
I confirmed this by calling scrollToRowAtIndexPath: then a selectRowAtIndex:animated:scrollPosition: wrapped in a perfomSelector:afterDelay:
How do I fix this without using perfomSelector:afterDelay: ?

UITableView cell selection disabled and UITableViewHeader Enabled

I am adding a my header view to
self.tableView.tableHeaderView=headerView;
This tableView has 10 cells.
I want to disable the cell Selection but, headerView touch events must be enabled.
To achieve this I added the following code:
self.tableView.userInteractionDisabled=YES;
self.headerView.userInteractionDisabled=NO;
self.headerView.exclusiveTouch=YES;
Where I am wrong?
Basic idea of implementation is , If headerView is enabled then cell selection is disabled and vice-versa.
I'm not sure that I completely understand what you are asking, but if you want to avoid seeing any cell highlighting set the UITableViewCell selectionStyle to UITableViewCellSelectionStyleNone. That's what I do, and then don't implement the UITableViewDelegate method tableView:didSelectRowAtIndexPath:.
I've never tried it, but I'm pretty sure you can also disallow the selection of any rows by setting the UITableView property allowsSelection to NO.
The userInteractionDisabled property of the table view should be set to NO. Otherwise your headerView, which is a subview of tableView, will not get touch events. Setting a superview's userInteractionDisabled property to no disables touch events for all of its subviews.

UITableViewCell with UIButton is not scrolling

I have UITableView. Each UITableViewCell content UIButton. UIButton's frame is equal to UITableViewCell's frame. But scrolling is not working when I tapped on button and scroll, only when I tapped on space between cell's scrolling is working. How can I fix this problem?
You need to disable the user interaction of your button. Then it will not take the touch event and you will be able to scroll properly.
The button is not forwarding the touch to the cell.
The easiest solution would be to give the cell the appearance of a button and use the normal delegate methods.

Resources