1 前言
今天我们来学习一下怎样从UITableView中滑动删除Cell数据。
2 代码实例
ZYViewController.h
[plain]
#import
@interface ZYViewController : UIViewController
@property(nonatomic,strong) UITableView *myTableView;
@property(nonatomic,strong) NSMutableArray *arryOfRows;
@end
#import
@interface ZYViewController : UIViewController
@property(nonatomic,strong) UITableView *myTableView;
@property(nonatomic,strong) NSMutableArray *arryOfRows;
@end
ZYViewController.m
[plain]
@synthesize myTableView;
@synthesize arryOfRows;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];//设置列表样式为简单的样式 还有一个样式为UITableViewStyleGrouped为分组模式 UITableViewStylePlain为普通的样式
self.myTableView.delegate = self;//设置代理为自身
myTableView.dataSource = self;//设置数据源为自身
self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;//确保TablView能够正确的调整大小
arryOfRows = [[NSMutableArray alloc] initWithObjects:@"a",@"b",@"c", nil];//初始化表格数据
[self.view addSubview:myTableView];
}
//设置每行的高度
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CGFloat result = 20.0f;
if ([tableView isEqual:self.myTableView]) {
// result = 40.0f;
result = 80.0f;
}
return result;
}
////允许数据源告知必须加载到Table View中的表的Section数。
//-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
// NSInteger result = 0;
// if([tableView isEqual:myTableView]){
// result = 3;//一共三个section
// }
// return result;
//}
//设置每个Section呈现多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.arryOfRows count];
}
//每行对应的数据
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *result = nil;
if ([tableView isEqual:myTableView]) {
static NSString *tableViewCellIdentifier = @"MyCells";//设置Cell标识
result = [tableView dequeueReusableCellWithIdentifier:tableViewCellIdentifier];//通过标示符返回一个可重用的表视图单元格对象
if (result == nil) {
result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewCellIdentifier];//初始化一个表格单元格样式和重用的标识符,并将它返回给调用者。
}
//indexPath.section 表示section的索引 indexPath.row表示行数的索引
result.textLabel.text = [arryOfRows objectAtIndex:indexPath.row];
}
return result;
}