开发者

How to impliment UISwipeGestureRecognizer on UITableView cell?

开发者 https://www.devze.com 2023-04-04 15:47 出处:网络
How can i add swipe gesture in my table view cell?开发者_运维知识库 i am using custom cell in tableview and i have to delete that row from table so please guide me how can i use this swipe gesture in

How can i add swipe gesture in my table view cell?开发者_运维知识库 i am using custom cell in tableview and i have to delete that row from table so please guide me how can i use this swipe gesture in table view?


Absolutely the same as in any other view. Insert this code either in your custom cell's init or in cellForRowAtIndexPath method of your UITableViewDataSource delegate.

UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:myTableViewController action:@selector(removeCell:)];
recognizer.direction = UISwipeGestureRecognizerDirectionLeft;
recognizer.numberOfTouchesRequired = 1;
[self addGestureRecognizer:recognizer];
[recognizer release];


You have to implement two delegate method.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
     return YES;
}

and other method where you have to perform you editing or deleting code.

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete)  {
        //write delete code.
        [arry removeObjectAtIndex:indexPath.row];

        [Table reloadData];
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号