TableView the timing of the Accessorybuttontappedforrow method execution

Source: Internet
Author: User

Hit the code when you encounter this problem, do not lazy, write down for reference.

When you are in IB to TableView in the accessory (note, I said the cell accessory, not the cell) to create segue, if you in the VC at the same time implement the following 3 methods, call the Order is God horse!?

//1func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath)//2overrideString, sender: Any?) -> Bool//3override func prepare(for

The answer is: 2,1,3

If you use Shouldperformsegue, then you cannot take the segue itself in the method, you must cooperate with the Prepare method.

If you need to get the index of the cell that was pressed accessory in the Prepare method, then you cannot use Tableview.indexpathforselectedrow, Because you segue bind the cell's accessory instead of the cell, the return value you get is nil.

To solve the above problem is very simple prepare the second parameter sender is:

letas! UITableViewCelllet selectedRow = tableView.indexPath(for: cell)!.row

If you are customizing the cell's Accessory button, you may need to return to the following method:

tableView.indexPathForRow(atpoint)

Here is the equivalent of the OBJC code, leaving a thought:

*touches = [event allTouches];  *touch = [touches anyObject];  CGPoint currentTouchPosition = [touch locationInView:self.contactsTableView];

TableView the timing of the Accessorybuttontappedforrow method execution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.