简体   繁体   English

如何通过轻按单元格在uitableviewcell中显示滑动按钮?

[英]How to display swipe button in uitableviewcell by tap on cell?

I have tried to call the functions manually by programming, those functions are 我试图通过编程手动调用函数,这些函数是

    -(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES; //tableview must be editable or nothing will work...
}

When i call the functions it works inside the functions for example the editActionsForRowAtIndexPath function but doesnt show the button. 当我调用函数时,它在函数内部起作用,例如editActionsForRowAtIndexPath函数,但不显示按钮。 Any clue or help will be highly appreciated. 任何线索或帮助将不胜感激。

Did you forgot to call [self.tableView setEditing:YES animated:YES]; 您是否忘记了调用[self.tableView setEditing:YES animated:YES]; ?

Why do you use above 3 methods for swipe and deleting the table view cell? 为什么要使用上述3种方法来滑动和删除表格视图单元格? Only one method is enough for doing this.I created sample one for your question.It works fine. 只需要一种方法就可以了,我为您的问题创建了一个样本,效果很好。

ViewController.h ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) IBOutlet UITableView *tableViewEdit;

@end

ViewController.m ViewController.m

#import "ViewController.h"

@interface ViewController (){
    NSMutableArray *arr;
}

@end

@implementation ViewController

@synthesize tableViewEdit;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    arr = [[NSMutableArray alloc]initWithObjects:@"iOS",@"Android",@"Windows",nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//UITableViewDataSource methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  arr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *strCell = @"cell";
    UITableViewCell *cell = [tableViewEdit dequeueReusableCellWithIdentifier:strCell];
    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCell];
    }

    cell.textLabel.text = arr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle == UITableViewCellEditingStyleDelete)
    {
        [arr removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}

When I run the application first it shows the below data 当我第一次运行该应用程序时,它显示以下数据

在此处输入图片说明

Then when I swipe the table view 然后,当我滑动表格视图时

在此处输入图片说明

Finally I delete the data from the table view and the table view 最后,我从表格视图和表格视图中删除数据

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM