Disabling touch interaction of Spaces between Custom table view cell - ios

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

Related

Can I make only a certain portion of a tableview clickable?

I'm trying to add a like button in a tableview but If I click anywhere in the tableview, it immediately goes to the detailsVC. I want to make it so when you click that like button, only that is selected. Is there a way to make it so only the top half of the tableview can be selectable or have interaction enabled, and put the button below? Or is there another way to do this. Is there a workaround or a method I am missing. Thanks
Are you sure the UIButton is added correctly to table view cell's .contentView?
Can you see it? If so it will intercept the touch events and tableview's delegate didSelectRowAtIndexPath won't get called (even if there's no handling connected to the button). If you want make the button occupy specific area of the cell you can either:
Design your cell in Interface Builder and make it a part of Storyboard/Xib
Do autolayout of the cell programmatically
Override cell's layoutSubviews and manually setup the frames

Limit the cell selection area in uitable view

I have a custom cell in table view, which allows multiple selection. Can i limit the cell selection area of a custom cell. Can i enable selection area to only the left side of the cell not the entire cell.
Please share your thoughts.
In your UITableViewCell subclass override pointInside:withEvent: method and return YES only for the points inside the area you want.
Yes, the way you want can be implemented, You make a button on left side of your cell. So Whenever you click on button your selected are will be enabled.
You do not need to implement any logic in UITableView DidSelectRowForIndexPath.
Need to manage in your button.
You can add a UIButton on the left side of your cell, by this way you can easily handle the selection of UIButton by using its tag value.
In the custom tableView Cell. The are which you want to be non clickable add a button to that area and if a tap is registered in the button the didselectitem at indexPath would not be called I have done a similar implementation myself.
My TableView Implementation

Button not responding in Table View Cell [duplicate]

This question already has answers here:
Programmatically send to front/back elements created from interface builder
(2 answers)
Closed 10 days ago.
I am using a .xib for my cell in a table view. Inside the .xib, is another view (cellBackgroundView), and a button. When I run the app, and click the button, it does not respond at all. Instead, it calls the tableView's didSelectedRowAtIndexPath method which brings another view controller.
Using Xcode's Debug View Hierarchy, I discovered that I have a view overlaying the all the buttons (see pic attached: this overlaying view is highlighted). This view (called backgroundView) that is overlaying my button is a View, within a view. I have a feeling when you place a view in a view, and put a button in the initial view, the button isn't called because its below the view hierarchy.
How do I fix this issue? Is there a way to move background view to the back of the view heirarchy so that the buttons will be responsive?
Debug View hierarchy:
Structure of .xib
Two things that you could check
1) Do you have a delegate method for cell height and is the height returned correct? Unless you have Clip Subviews on for the UITableViewCell, the contents of the cell can be visible outside it's frame, but the parts that are outside the cell's frame are not registering user interactions.
2) Is some other view element higher in the hierarchy (lower in the XCode listing you posted) overlapping the button? iOS Simulators Debug -> Color Blended Layers can help spotting this.
Edit:
If I interpret the added screenshots correctly, you probably have the issue mentioned in the option 1) above. If the other elements showing in the screenshot are those listed as subviews of the Cell Background View they are mostly outside the parent view's frame and thus don't receive touch events. If the background view's frame is correct, then you might want to move the other elements as children for Feed Cell directly.
Also, the element listing suggests that you are using plain UIView as the parent element. I don't know the inner workings of your application, but if you only use this view in a UITableViewCell you might want to consider making the parent view a Table View Cell in the xib. This will reduce some bloat and allow you to configure some properties for the cell in the xib.
Maybe you forgot assign your Button to code
I assume Feed Cell is a subclass of UITableViewCell, and cellBackgroundView is the property contentView of this cell.
If so, the cells property backgroundView should be behind your cellBackgroundView (the docs say: UITableViewCell adds the background view as a subview behind all other views and uses its current frame location.).
You could set the cells property backgroundView = nil, and see whether it is still there in the view hierarchy. If so, you do add a custom backgroundView on top of the other cells views somewhere.
To check this, you could read out the subview hierarchy of your cell in your method tableView:cellForRowAtIndexPath: with something like NSArray *svs = cell.subviews; (assuming cell is the tableViewCell) and set a breakpoint behind this statement.
svs usually contains first the UITableViewCellContentView, and above it the _UITableViewCellSeparatorView. The cells backGroundView will not be shown. You could check there your view hierarchy.
If by chance there is a custom backgroundView on top, you could - as a workaround, not a solution - bring the contentView to the front by sending to the cell bringSubviewToFront: with the contentView as argument. Then the button should respond.
In your Structure of Xib Place your button below the view that is first the view is added to superView then the Button, then your button will work.
or you can code
-(void)viewDidLayoutSubviews{
[self.view insertSubview:yourButton aboveSubview:cellBackgroundView];
}
Hope it will help.
Do you add an UIButton by code?
If so you should ensure you addSubview: into cell.contentView and not into cell.
Also you could try to apply CellBackground class to a view inside contentView, not directly to contentView.
you can use this method.
[cell.contentView bringSubviewToFront:yourButton];
after that if you want to back in background then tou can use sendSubviewToBack: method .
After adding this methods your button is not responding set the userIntractionEable of button's superview.

UITableView edit mode shifting of cell

I have an UITableView with a custom cell and a custom button for the entering the edit mode as well as a custom accessory button. Everything is working but I want the UIImageView with two other UILabels adjacent to it within the cell to be at its X position but only want the "ADD QTY" UILabel to shift its X position to make room for the accessory button.
If you don't want the contents of the UITableViewCell to shift, implement the method
- tableView:shouldIndentWhileEditingRowAtIndexPath:
and make it return NO. This method is a part of UITableViewDelegate.
To make the ADD QTY move left, you will need to make a custom animation, I think. Animate the label for ADD QTY in this method
- tableView:willBeginEditingRowAtIndexPath:
There is a similar question here. Check it out and tell me if it works. Cheers!

Making UITableView with delete buttons visible for all cells

Normally one can create a UITableView with cells that allow the user to reveal the red "Delete" button on the right side of the cell by swiping that cell or by tapping a knob visible on the left side of the cell.
However, I would like to create a UITableView which has all cells with visible "Delete" button by default (no swiping / tapping knob necessary). Is it possible to do utilizing the standard Cocoa Touch methods?
(I know I can define my own button in a custom UITableViewCell, but I'd rather use a standard method if possible...)
Call [tableView setEditing:YES animated:NO] in your view controller's -viewDidLoad implementation.
Well.. NO. Actually the red minus on the left side is the constant-visible solution in Cocoa. I don't believe you get the right delete button without ugly hacking of the tableView.
So probably you have to make a custom solution. But you need no subclassing here. You can just use a regular UITableViewCell and set a UIButton as the accessoryView.

Resources