






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