美文网首页
tableview多个区cell右滑删除

tableview多个区cell右滑删除

作者: 奇怪的知识增加了 | 来源:发表于2017-03-07 13:53 被阅读0次

////  ViewController.m//  testcellobe////  Created by 密码123 on 17/2/11.//  Copyright © 2017年 密码123. All rights reserved.//#define SCREEN_WIDTH    [UIScreen mainScreen].bounds.size.width#define SCREEN_HEIGHT  [UIScreen mainScreen].bounds.size.height#import "ViewController.h"@interface ViewController ()@property (nonatomic,retain)UITableView *tvContent;

@property (nonatomic,retain)NSMutableArray *arraySection;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

[self initWithcontrols];

[self initwithDatasource];

}

-(void)initwithDatasource

{

//大数组里面放小数组,大数组数量作为区数,小数组数量为row数

_arraySection=[NSMutableArray arrayWithCapacity:0];

NSMutableArray *arrayTest=[NSMutableArray arrayWithCapacity:0];

NSMutableArray *arrayTestTwo=[NSMutableArray arrayWithCapacity:0];

for (int i=0; i<2; i++)

{

[arrayTest addObject:[NSString stringWithFormat:@"%d",i]];

[arrayTestTwo addObject:[NSString stringWithFormat:@"%d",i]];

}

/*重要的是大数组里面的小数组名字不能一样

[_arraySection addObject:arrayTest];

[_arraySection addObject:arrayTest];

*/

[_arraySection addObject:arrayTest];

[_arraySection addObject:arrayTestTwo];

}

-(UITableView *)tvContent

{

if (!_tvContent) {

_tvContent = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];

_tvContent.dataSource = self;

_tvContent.delegate = self;

_tvContent.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

}

return _tvContent;

}

-(void)initWithcontrols

{

[self.view addSubview:self.tvContent];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

#pragma mark - UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return _arraySection.count;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return [_arraySection[section] count];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

return 44;

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

return 42;

}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{

UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 42)];

view.backgroundColor=[UIColor clearColor];

UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, 32)];

label.backgroundColor=[UIColor clearColor];

label.font=[UIFont systemFontOfSize:13];

label.textAlignment=NSTextAlignmentCenter;

label.textColor=[UIColor grayColor];

label.text=@"18:12";

[view addSubview:label];

return view;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *flag = @"systemMassageCell";

UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:flag];

if (!cell)

{

cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:flag];

}

if (indexPath.row<[_arraySection[indexPath.section] count])

{

cell.textLabel.text=[_arraySection[indexPath.section] objectAtIndex:indexPath.row];

}

return cell;

}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{

return YES;

}

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

{

UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {

//删除row,首先把row从数组中移除

[_arraySection[indexPath.section] removeObjectAtIndex:indexPath.row];

//接着做移除row操作

[self.tvContent deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

//移除完row如果row的数量为0,就要把区删除

if ([_arraySection[indexPath.section] count]<=0)

{

//删除区,首先把section从数组中移除

[_arraySection removeObjectAtIndex:indexPath.section];

//接着做移除section操作

[self.tvContent deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationLeft];

}

}];

return @[deleteRowAction];

}

@end

相关文章

网友评论

      本文标题:tableview多个区cell右滑删除

      本文链接:https://www.haomeiwen.com/subject/oaloittx.html