Button in UITableView Header Only Works 1/3 of the Time - ios

I have a "Create Tribe" "button" in the header of a UITableView. It's not an UIButton, but 3 views ("+" icon, label, and a background view)
I have added a tap gesture recognizer to the background of the button (CreateTribeButton layer in the hierarchy).
The problem is, tapping on the button only works 1/3 of the time. I have to tap a few times in order for it to trigger the desired action.
I have no idea where I should begin debugging.
This view controller is embed in a pager - https://github.com/xmartlabs/XLPagerTabStrip
I have tried disabling the canCancelContentTouches and delaysContentTouches in the table view (which is actually a scroll view), and its parent pager's content view (which is also a scroll view). It doesn't actually solve the problem.
Is there an easy way for me to find out on which view did the touch get "eaten" up?

The easiest way to tell which section was tapped is in the
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){}
Function. You could simply implement
print(indexPath.row)
to find out what section was tapped. I would put a break point on this function and step through it. Additionally, you could programmatically call the buttons functionality when the right row is pressed. For example:
switch indexPath.row {
case 0:
doButton()
case 1:
/// do something else
default:
// break
}
That will help you figure out what is going on.

Related

Swift - Is it possible to move to the next cell in tableview with the click of a button?

I have a tableview and custom cells which take up the fullscreen however I am wondering if upon click of a button in my cells I could move to the next cell automatically, kind of like how when you scroll more than half in TikTok it automatically goes to the next video. Aside from manually scrolling at a faster speed which is the only way I can think of, is there any other way to automatically move to the next cell, impersonating a full scroll?
Thanks for the help.
You can use that method:
tableView.scrollToRow(at: indexPath, at: .top, animated: true)
Approach 1
Just delegate your tap event (IBAction of the button inside of your custom cell) back to your UIViewController that holds the UITableView reference and call scrollToRow(at:at:animated:).
Approach 2
You could also handle with tableView(_:didSelectRowAt:). Next cell would be the indexPath.row + 1 with scrollToRow(at:at:animated:)

Dynamic textfield not getting cached in TableView didSelectRowAt method

Actually, I am implementing a dynamic Question-Answer form in which table cells which load with dynamic fields (like., Drop Down, TextField, GridView, etc.,) For this in the cell I created one label for Question and one blank view to render dynamic content of Answer field. If I clicked on the label or on the cell I am able to reach to didSelectRowAt TableView method but if I clicked on dynamic content (like., Inside textField or on the button) not able to reach on didSelectRowAt. Not getting how to tackle this.
Try setting userInteractionEnabled = NO
This will avoid all the touch events for that particular control. and the events will be handled by the next responder in chain.

How to edit tableview cell on TAP instead of swipe - Swift

I have a UITableViewController and at the moment, the user can swipe left on the cell to reveal an 'add to favourites' button. I would now like to add an icon to the cell that the user can tap to reveal the this button (does the same thing as swiping to the left). This will just act as a visual aid for the users who do not know swiping left will do the same thing.
For my TableView, I use the following code to 'close the cell' when the user has pressed the button:
itemsTableView.setEditing(false, animated: true)
From this I assumed that to manually open the cell I would simply call:
//Get cell
let cell:TableViewCell = itemsTableView.cellForRowAtIndexPath(NSIndexPath(forRow:
sender.tag, inSection: 0)) as! TableViewCell
//Open cell to reveal button
cell.setEditing(true, animated: true)
Unfortunately this does not work. So I would like to know what is the function that is called when a user swipes to the left of a cell and how can I replicate it when a user taps on the icon?
I don't think you're going to be able to do it, sorry. This feature relies on the UITableView's pan gesture recognizer. When the user pans sideways, the table view checks the editActions... delegate method and, if we have edit actions, inserts a private confirmation view behind the cell and allows the cell to be moved to reveal it. You can't trigger that behavior programmatically as far as I can tell.
This would be a reason — one of many very good reasons — for sticking with a third-party scrollable cell class. In this way, the cell is a horizontal scroll view and you just scroll it in code.

Can't select the tableView Cell because of textView

I have got a TableView and each cell is covered by a Text View.
I think that because of this reason I can't select any cell.
Whenever I try to print it I don't get anything:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var i = Int()
i = indexPath.row
print("i=\(i)")
}
**Its print and not println because of Swift 2.0.
Please ensure following:
You are correctly setting your view controller as delegate of the table view.
Your touches are intercepted by text view and are not reaching underneath cell. To fix this set userInteractionEnabled to false in cellForRowAtIndexPath: and set it back to true in didSelectRowAtIndexPath:.
Take a look at UITextView's userInteractionEnabled, multipleTouchEnabled, and exclusiveTouch. Those properties alter whether or not a view receives touches; if it doesn't, the touches get passed to the view behind it (or at least I think that's how it works). I forget where it is, but there's also a method that's called to ask a view which view should be the target of a specific touch, which would allow you to explicitly tell it to send them to the underlying view.
However, from a design perspective, I would re-evaluate having a UITextView on top of a UITableViewCell - the text view is a scroll view and the cell is in a scroll view, so they will always conflict. UILabel is generally more appropriate for putting in a text view.
For those who are using storyboard just uncheck User Interaction Enabled and Multiple Touch as showing below in the screenshot:
I have listed below points for your solution.
you need to check delegate is connected to your view controller.
next you need to check table view selection is enabled for your table view you can check it from table view attributed inspector.
next make sure user interaction is enabled for your text view.
Hope it will be the right solution for you.

How do you change image in all uitableview cells

I have a UITableView with a play icon in each cell. When you tap on that play icon it changes to a "Pause" icon. That works marvellously, but if you tap on an icon in another cell, how do I notify the previous cell to change back into a "play" icon? Basically I think I need a way to notify all the UIImage views inside cells at once. All of them for the whole table
You should have a var for playing indexPath.
When tap on another cell:
For visible cell: checking in array of visible cells with playing indexPath. -> if indexPath is in visible cell change 'pause' to 'play'
Then -> assign playing indexPath to current cell.
For unloaded cell: check it in cellForRowAtIndex. if indexPath != playingIndexPath -> 'play'
Sorry, I can't write ios code because you didn't show any. Hope this help
Three simple steps which you need to do in the sequence
1.In tableView:cellForRowAtIndexPath: datasource method, set the default image of the imageView as 'play'.
2.When the imageView of any cell is tapped, call the tableView reloadData method, which will reload the imageView of all cells to the default image.
3.Now change the imageView of the tapped cell to 'pause'.
As the second step sets the imageView of other cells to default, no separate notification is required. Happy coding :)

Resources