您的位置:首页 > 产品设计 > UI/UE

iOS UITableView 便捷编写

2015-12-30 13:58 495 查看
- (void)_prepareUI

{

CGRect rect = CGRectMake(MainSreenOrigin_X,MainSreenOrigin_Y,MainScreenSize_W,MainScreenSize_H);

_myTableView = [[UITableView alloc] initWithFrame:rect style:UITableViewStylePlain];

_myTableView.delegate = self;

_myTableView.dataSource = self;

_myTableView.scrollEnabled= YES;

_myTableView.backgroundColor=[UIColor clearColor];

[self.view addSubview:_myTableView];

// 改变 tableView的背景色

UIView * backColorView = [[UIView alloc] init];

backColorView.backgroundColor = PView_BGColor;

_myTableView.backgroundView = backColorView;

// 去掉多余的分割线

UIView * footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MainScreenSize_W, 0)];

footerView.backgroundColor = [UIColor clearColor];

_myTableView.tableFooterView = footerView;

[self.view addSubview:_myTableView];

}

#pragma mark ---

#pragma mark --- UITableViewDelegate ---

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 1;

}

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

{

return 6;

}

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

{

static NSString *cellIdentifier = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil) {

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

}

cell.selectionStyle=UITableViewCellSelectionStyleNone;

// [cell addCellData:nil];

return cell;

}

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

{

return 80;

}

自定义cell里面的方法

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {

        self.selectionStyle=UITableViewCellSelectionStyleNone;

        

    }

    return self;

}

/*删除用到的函数*/

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

    if (editingStyle ==UITableViewCellEditingStyleDelete){

        [_classList removeObjectAtIndex:indexPath.row];  //删除数组里的数据

        [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];  //删除对应数据的cell

    }

}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return UITableViewCellEditingStyleDelete;

}

/*改变删除按钮的title*/

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return @"移除";

}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath

{

    //可添加多个

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

        [_classList removeObjectAtIndex:indexPath.row];  //删除数组里的数据

        [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];  //删除对应数据的cell

        

    }];

    deleteRowAction.backgroundColor = PView_OrangeColor;

    return  @[deleteRowAction];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: