tableview多个区cell右滑删除










////  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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,088评论 3 38
  • 哦吼吼,又研究了几天,把FMDB这个封装好的数据库搞定了,写了个简单的例子,基于FMDB的添删改查操作,界面很一般...
    lichengjin阅读 562评论 0 0
  • 1.badgeVaule气泡提示 2.git终端命令方法> pwd查看全部 >cd>ls >之后桌面找到文件夹内容...
    i得深刻方得S阅读 4,752评论 1 9
  • 作者唯一QQ:228544117。。。。。 =========后面的都要新建一个文章 AppDelegate.h ...
    CC_iOS阅读 948评论 0 0
  • 你等我一朵花开的时间 我回赠你怒放千年 谁说不败是个神话 为你 我将乾坤扭转 我年轻的呼啸直冲云端 上帝睁开紧闭的...
    粉妖阅读 449评论 2 6