如何通过点击单元格在 uitableviewcell 中显示滑动按钮?
How to display swipe button in uitableviewcell by tap on cell?
我试过通过编程手动调用函数,那些函数是
-(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...
}
当我调用函数时,它在函数内部工作,例如 editActionsForRowAtIndexPath 函数,但不显示按钮。
任何线索或帮助将不胜感激。
你是不是忘记打电话给 [self.tableView setEditing:YES animated:YES];
了?
table视图单元格为什么要用上面3种方式滑动删除?
只有一种方法足以完成 this.I 为您的 question.It 创建的示例 1 工作正常。
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) IBOutlet UITableView *tableViewEdit;
@end
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];
}
}
当我 运行 应用程序首先显示以下数据时
然后当我滑动 table 视图时
最后,我从 table 视图和 table 视图
中删除了数据
我试过通过编程手动调用函数,那些函数是
-(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...
}
当我调用函数时,它在函数内部工作,例如 editActionsForRowAtIndexPath 函数,但不显示按钮。 任何线索或帮助将不胜感激。
你是不是忘记打电话给 [self.tableView setEditing:YES animated:YES];
了?
table视图单元格为什么要用上面3种方式滑动删除? 只有一种方法足以完成 this.I 为您的 question.It 创建的示例 1 工作正常。
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) IBOutlet UITableView *tableViewEdit;
@end
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];
}
}
当我 运行 应用程序首先显示以下数据时
然后当我滑动 table 视图时
最后,我从 table 视图和 table 视图
中删除了数据