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.