TableView基本设置

2018-02-05  本文已影响6人  软工官博

    //去掉尾部空白
    _tableView.tableFooterView = [[UIView alloc] init];

    //去掉分割线
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

   [tableView deselectRowAtIndexPath:indexPath animated:YES];// 取消选中

UITableViewCell*cell=[tableViewcellForRowAtIndexPath:indexPath];

//去除点击效果

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

//去除选中效果

//在didSelectRowAtIndexPath中

cell.selected=No;

#pragma mark - 分割线对齐 方法一

    if ([_tableView respondsToSelector:@selector(setSeparatorInset:)])

    {

        [_tableView setSeparatorInset:UIEdgeInsetsZero];

    }

    if ([_tableView respondsToSelector:@selector(setLayoutMargins:)])

    {

        [_tableView setLayoutMargins:UIEdgeInsetsZero];

    }

//在UITableView的代理方法中加入以下代码 分割线对齐

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{

    if ([cell respondsToSelector:@selector(setSeparatorInset:)])

    {

        [cell setSeparatorInset:UIEdgeInsetsZero];

    }

    if ([cell respondsToSelector:@selector(setLayoutMargins:)])

    {

        [cell setLayoutMargins:UIEdgeInsetsZero];

    }

}

#pragma mark - 分割线对齐 方法二

_tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);

#pragma mark - 侧滑删除

//先要设Cell可编辑

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

    return YES;

}

//定义编辑样式

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

    return UITableViewCellEditingStyleDelete;

}

//修改编辑按钮文字

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

    return @"删除";

}

//设置进入编辑状态时,Cell不会缩进

- (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {

    return NO;

}

//点击删除

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

    //在这里实现删除操作 //删除数据,和删除动画

    //删除成功

    [self.dataArray removeObjectAtIndex:indexPath.row];

    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

}

上一篇下一篇

猜你喜欢

热点阅读