Long press Cell to select, Cell to select

Source: Internet
Author: User

Long press Cell to select, Cell to select

Beginner, Do not spray ~

Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------------------

In the past few days, a long-pressed Cell deletion function has been implemented.

Added a UILongPressGestureRecognizer long-pressed gesture, which is directly written in the gesture method.

If (sender. state = UIGestureRecognizerStateBegan ){

}
However, I encountered a problem at this time. I used the following method to find the cell's indexPath.

CGPoint point = [sender locationInView: self. tableView];
NSIndexPath * indexPath = [self. tableView indexPathForRowAtPoint: point];

The problem is that I usually instantiate a cell, and then use indexPath. section and indexPath. row to determine the cell at different locations and load data for it. But now I only know how to find the corresponding cell of indexPath. Well, it's hard for me, a beginner, to have this problem for the first time. Then we can see-(NSArray *) visibleCells; this method is used to obtain all cells displayed on the screen.

So happy, get the cell through indexPath. row, and finally get it done.

NSArray * cells = [self. tableView visibleCells];
MapTableViewCell * cell = cells [indexPath. row];

The first few cells have no problems. However, when the figure starts to scroll and then the cell is selected, various crashes occur. After a long tangle, the cause of the crash is very simple, and the array is out of bounds. Because indexPath is incremental and the number of cells is fixed, naturally, indexPath. row is greater than cell. count. The solution is very easy. cell. tag = indexPath. row; find the crux and solve the problem!

If (sender. state = UIGestureRecognizerStateBegan ){
CGPoint point = [sender locationInView: self. tableView];
NSIndexPath * indexPath = [self. tableView indexPathForRowAtPoint: point];
If (indexPath = nil) return;

NSArray * cells = [self. tableView visibleCells];
NSInteger tag = (UITableViewCell *) [cells firstObject]). tag;
UITableViewCell * cell = cells [indexPath. row-tag];
}
The final result is like this. I got the cell with a long-pressed gesture.

Related Article

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.