Is it possible to disable didSelectRowAtIndexPath in part of a row in a UITableView?

I have a UITablaView in my Objective-C application. I have custom cells with label and UIImageView . I want to disable part of the lines to disable didSelectRowAtIndexPath when users click on this part of the line.

I want it:

enter image description here

Is it possible?

+5
source share
6 answers

Here is the simplest and most elegant solution that I can come up with.

I believe you should have a CustomCell that contains an IBOutlet for ImageView on the left. You can use the hitTest method to solve your problem :)

In your CustomCell, you can assume that this is the MyTestCell class, write this,

 override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { if self.myImageView.frame.contains(point) { return nil } return super.hitTest(point, with: event) } 

Where myImageView is the IBOulet imageView on the left side of your cell :)

All I do is check where the user clicked, if the touch point is inside the frame of the imageView cells, you return zero as a view. When you return zero, the events stop propagating to the parent views and finally never reach the cell, so didSelectRowAtIndexPath never called,

On the other hand, you have to pass the touch to the next view, and you do it simply by calling the same method on your super iOS, in the end, it will track it back to the cell and didSelectRowAtIndexPath triggers

Hope this helps :)

+6
source

You can do a simple trick (without writing code) to solve this problem by adding a button that covers the part you want to disable. Obviously, the button should not have any text or background color (it should be a pure color), also be sure to add the appropriate restrictions to make sure that it covers the desired part of the cell.

Thus, when a button is pressed, nothing should happen, and didselectRow should not be called, because the actual touching event should be attributed to the button, and not to the line.

Hope this helps.

+1
source

You can simply change the appearance of the image width constraints to 0 . Where do you want to perform the action. For instance:

1) If you use a button that closes the image.

2) If you take the navigation bar, then you make a button there and perform an action.

hope this helps.

0
source

This is a very simple trick. Add a button on a part of the profile picture means the red part, as shown in the figure (provided by you). And do not do this when you press the button.

Happy coding !!!!

0
source

Just place the button over the green area and set a tag for each button. Onclick you can execute your functionality with a tag. Like this

inside

 func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ let obj: AnyObject = self.dataList.objectAtIndex(indexPath.row); cell?.populateCellWithData(obj, indexPath: indexPath) cell?.destinationLabel.userInteractionEnabled = true let destRecognizer : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "DestLabelTapped:") destRecognizer.numberOfTapsRequired = 1 destRecognizer.delegate = self cell?.destinationLabel.addGestureRecognizer(destRecognizer) } 

and in DestLabelTapped you can build your functionality

Another way Just set up two Tableviews and scroll them next to each other, and one table will be selected and the other not. (do not do that)

0
source

In my understanding, tricks with buttons and other UIElements that cover the content are not the right way to solve the problem. Since you will need additional manipulations with them in the Storyboard, if you need to make dynamic content, if you work with restrictions and many more situations when you need to control your content and + artificial cover. There are a few things to do:

  • Set UITableView to No Selection
  • Put the second part that you want to be active in in the UIView . This UIView will be a content container.
  • Add UITapGestureRecognizer to UIView

     UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectCellAction:)]; 
  • Add IBAction , where you can do everything you need.

     - (IBAction)selectCellAction:(id)sender { // do what you need } 

And that’s all.

0
source

Source: https://habr.com/ru/post/1261245/


All Articles