tableview多個區(qū)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

{

//大數(shù)組里面放小數(shù)組,大數(shù)組數(shù)量作為區(qū)數(shù),小數(shù)組數(shù)量為row數(shù)

_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]];

}

/*重要的是大數(shù)組里面的小數(shù)組名字不能一樣

[_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從數(shù)組中移除

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

//接著做移除row操作

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

//移除完row如果row的數(shù)量為0,就要把區(qū)刪除

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

{

//刪除區(qū),首先把section從數(shù)組中移除

[_arraySection removeObjectAtIndex:indexPath.section];

//接著做移除section操作

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

}

}];

return @[deleteRowAction];

}

@end

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件,我們平時使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,282評論 3 38
  • 哦吼吼,又研究了幾天,把FMDB這個封裝好的數(shù)據(jù)庫搞定了,寫了個簡單的例子,基于FMDB的添刪改查操作,界面很一般...
    lichengjin閱讀 662評論 0 0
  • 1.badgeVaule氣泡提示 2.git終端命令方法> pwd查看全部 >cd>ls >之后桌面找到文件夾內(nèi)容...
    i得深刻方得S閱讀 4,968評論 1 9
  • 作者唯一QQ:228544117。。。。。 =========后面的都要新建一個文章 AppDelegate.h ...
    CC_iOS閱讀 1,182評論 0 0
  • 你等我一朵花開的時間 我回贈你怒放千年 誰說不敗是個神話 為你 我將乾坤扭轉(zhuǎn) 我年輕的呼嘯直沖云端 上帝睜開緊閉的...
    粉妖閱讀 502評論 2 6

友情鏈接更多精彩內(nèi)容