Get button click on UITableView in Swift - ios

I have a simple table view which looks like this
Each button overflows some part to next cell (part of requirement).
I want that overflowing button's click event. How to get that ?
By default its taking it as cell click rather than button click if i click on that overflowing part.
I have created a dummy code which changes button color on click, so it is easy for someone to try it out. same layout.
Thanks
EDIT
Below is the original image , what i am trying to do , for simplicity sake i scaled it down to a button
Lets assume req is u cant change height and cant play around table view separator

You have to implement 'tableView:heightForRowAtIndexpath:` method and return greater value than it has now. Your cells don't have enough height to display buttons fully, and because of that some part of your button hides behind of next cell. You can be convinced easily by checking "Clip subviews" checkbox of your Cell Prototype in the interface builder, and if you will see that buttons will be clipped.
EDIT
Check this: https://github.com/arturdev/test

Subclass UITableView and override hitTest:withEvent: and figure out if point is within the frame of one of the buttons. Return the button if the point is within the frame or just return [super hitTest:point withEvent:event] otherwise.
You can use CGRectContainsPoint and [UIView convertRect/Point:to/fromView:] to make calculation easier.
hitTest:withEvent: is a way for the system to ask the outermost view who will receive the event at a given location.

Make UITableView seperator to None. draw 1 or 2 pixel height label with black background color in cell.
Make cell selection style none in cellForRowAtIndex.
cell.selectionStyle = UITableViewCellSelectionStyle.None
Manage proper autolayout or autoresizing mask.
Just tried with your code.
Download from link
Image

Related

Expandable view and text

I'm trying to create an iOS app and I want something like the pics below which when I tap the down arrow, the text and view expand.
I have no idea how it would be possible or even what should I call it.
Expanded one:
Edit:
StoryBoard:
There is a view for Video player top and a ScrollView down which contains an image and a tableview. what i want is that put the above feature(2 pics above) instead of that image. and scroll all image and tableview up to be behind of video player view
Using Autolaout you extend the height of your textview based on content size and your view
You need self sizing cell with tableview automatic dimension you can find tutorial for that
in label set number of lines 2 and line break mode to truncate tail initially ,
on tap of button action you need to set number of line 0 and line break mode world wrap , and use beginUpdate and endUpdate tableview
You have to add action (selector) to the button and in it you have to modify the frame and properties of containting view. Please post some code/storyboard for detailed answer.

iOS 8.3: hidden table row is still visible in the background

In order to hide a table row in some situations I set its height to 0. It works great but unfortunately I still see the row in the background. Can anybody explain this to me? How am I supposed to solve this? I believe I could switch the cell background color from Default (transparent) to something else but in some cases I might want to get the color from the table view itself (from the background)...
The cell is render beyond the bounds of the cell, you will need to clip the subviews.
Either in Interface Builder thick the clip subviews box of set the clipsToBounds to YES.

iOS: Combine two UIButtons to one UIButton

On the storyboard I have two UIButtons. In my UIViewController subclass I have one IBAction method for this buttons. One of this buttons has an image and hasn't got a title, another has the title but hasn't got the image. So, it's simply image that behaves as a button and ordinary button.
Both of this buttons call a method. The problem is that if I push one button, another doesn't highlight. Another problem that this buttons have padding and for me will be better if this padding will be touchable (I mean space between image button and title button to be clickable).
I know that I can subclass UIButton and make my buttons as I want, but maybe there is a way to make what I want without subclassing?
Thanks.
1)
Assign both buttons to properties.
When an IBAction fires, you can set one or both buttons to highlighted via those controls' "highlighted" properties, or via setHighlighted:.
2)
As for making space between the buttons touchable, set the alignment for the button graphic to left aligned or right aligned (the latter is what I've done in my example below) and these two separate buttons have edges that are touching.
3) Or...
You can cheat and have simply one button and put some space between your image and your button text, like this:
Yes, that's a whole bunch of spaces (which I've highlighted in green) before the word "Button".
Subclassing is a good thing. If you need a button with both an image and a title, then by all means create a subclass of UIButton that does what you want. You do that once, and then use it anywhere that you want. The alternative is to muck around with stacked duplicate buttons everywhere you want this look.
I found the most suitable variant for me and I think for all, who encountered the same problem. If you have one UIButton for image and another UIButton for text(title label), you can combine this two UIButtons to one Custom type UIButton. Then you can set image and title label text to this custom button. After that, use Insets to layout your image and title label.
For example, to set title label under image you must set Inset Left parameter as negative value and Inset Left parameter as positive value that is greater than image height. With Insets you can set proper image size too.
Using this way you don't need to handle highlight property changing by yourself.

How can I change UITableView height to its content height and still have clickable content?

So I have a tableview where I changed the size to match its content size with this code.
CGRect testF = table1.frame;
testF.size.height = table1.contentSize.height;
table1.frame = testF;
But when I try to click any of the items I can only click the top two or the section in the red, because that is the initial height of the table in the beginning.
Is there a way around this, to dynamically change its height and still keep it clickable?
I would think that the problem is not that you re sized the tableview, but that there is something that is blocking those touch events. Perhaps you have some other view in the way or the tableview is nested inside of another view which has a frame set to the tappable area that you see but has clips to bounds off so you are able to see the tableview outside of it. Try doing something like the following and see if it works, this should test my theory out:
table1.superview.frame = cgrectMake(table1.superview.frame.origin.x, table1.superview.frame.origin.y, testF.size.width, testF.size.height);
Add this under the code you showed above.

IOS: how to get called when press negative sign in uitableview

I have customized cells for a tableview. So when negative sign on the left of cell is pressed in editing mode, I need to redraw my cell to make space for delete button.(Also need to recover after press negative sign again.) So I wonder if there is a method get called when negative sign is pressed, or is there other method to implement this. Thanks.
Further update
Thank you for your answer. But the first suggestion seems not work. The cell's setDditing:animated: is called when tableview is set editing, not negative sign clicked.
Could you explain more about the second suggestion? I used drawRect to show cell's view. Is it suitable for drawRect?
You can do this in setEditing:animated: in your cell subclass. Make sure to also call the super implementation.
Alternatively, you can make sure that all of the elements in your cell have the correct autoresizing masks, and you may not have to write any custom code at all. Usually you set them to be a fixed distance from the left or right margin and to have a flexible width.

Resources