Technical implementation of gesture cell operation on UITableview

Source: Internet
Author: User

On a UITableview object, you need to implement the finger slide operation on each cell to delete or tick the cell.

In the editing status, the operation is to delete the cell; in the non-editing status, the operation is to tick.

First, add UIPanGestureRecognizer * panGestForDelete and * panGestForCheck to the custom UITableviewCell class to delete or tick the two objects respectively.

Next, declare the delegate in the cell class and use the delegate method to transmit the cell to the uitableview object.

Finally, in the selector implementation of the UIPanGestureRecognizer object, determine gesture in three states: UIGestureRecognizerStateBegan, UIGestureRecognizerStateChanged, and deleted.

Because each cell has an independent gesture, you can directly obtain the cell Object and operate on it.

In addition, during the UIPanGestureRecognizer operation, UITableview can also scroll. Therefore, use delegate to UITableview to set its scrollEnabled to NO. When UIPanGestureRecognizer ends, it is restored to YES.

This is the first implementation method. It can implement functions.

However, there are limitations. It adds objects to each cell and increases memory usage objectively. It also uses a large number of delegate callback operations to objectively increase CPU usage. In addition, the Code is complicated.

In another way, we can directly upload the UIPanGestureRecognizer object on the UITableview object. Each gesture operation must identify the cell on which the operation is performed.

First, add the UIPanGestureRecognizer * _ panGestForDeleteAndCheck; object to the custom UITableview class, and use self. editing to delete or tick the object.

Then, in the selector implementation code of the UIPanGestureRecognizer object, in the UIGestureRecognizerStateBegan state, the location of the touch point is obtained according to CGPoint location = [recognizer locationInView: self, use NSIndexPath * indexPath = [self indexPathForRowAtPoint: location] based on the location to obtain the table cell.

The Code is as follows:

 

Because the operation has been performed on the UITableview object, you can directly set scrollEnabled to NO.

 

This method is easier to implement, with much less memory usage, and at least the cell object is not that large.

 

In both methods, you must implement the delegate method.

This is because there are many gesture on the uitableview and it needs to be run together.

 

In the two methods, the cell processing method is the same, but the second method does not need to call delegate to operate on the cell.

 

Whether the UITableview is in the editing or non-editing status, the check boxes (if any) on each cell must be displayed. Therefore, you must set both cell. accessoryType and cell. editingAccessoryType.

Here, the delete operation is to draw a line between the text and delete the text in batches, so you need to implement this method as well.

-(UITableViewCellEditingStyle) tableView :( UITableView *) tableVieweditingStyleForRowAtIndexPath :( NSIndexPath *) indexPath

{

Return UITableViewCellEditingStyleNone;

}

Do not include built-in editing buttons such as delete and insert.

 

 

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.