UITableViewController的使用

如果整个程序界面都只是使用UITableView来搭建,一般需要如下步骤:

(1)向界面上拖一个UITableView

(2)设置数据源

(3)设置代理

(4)遵守代理协议

 上述过程相对繁琐,为了简化操作,直接使用UITableViewController,但要一定要注意:需要和主控制器类进行关联,还有就是不要把UIViewController当成UITableViewController使用
 
自定义TableViewCell(在ATDemoCell.m里):

+ (instancetype)cellWithTableView:(UITableView *)tableView

{

    static NSString *ID = @"cellID";

    ATDemoCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    if (cell == nil) {

        cell = [[ATDemoCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:ID];

    }

    return cell;

}

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

{

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

    if (self) {

      // 在此处定义控件

    }

   return self;

}

 在cellForRowAtIndexPath数据源里调用

ATDemoCell *cell = [ATDemoCell cellWithTableView:tableView];

分割线设置

//设置分割线
[tableView setSeparatorInset:UIEdgeInsetsMake(0, -67, 0, 0)];
//
分割线颜色 self.tableView.separatorColor = [UIColor redColor]; //隐藏分割线 self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//隐藏多余分割线
self.tableView.tableFooterView = [[UIView alloc] init];

取消选中样式

//取消选中样式
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;

设置背景色

// 改变整个tableView的颜色
   tableView.backgroundColor = [UIColor greenColor]

// 设置选中的背景色 UIView *selectedBackgroundView = [[UIView alloc] init]; selectedBackgroundView.backgroundColor = [UIColor redColor]; cell.selectedBackgroundView = selectedBackgroundView; // 设置默认的背景色 1 cell.backgroundColor = [UIColor blueColor]; // 设置默认的背景色 2 UIView *backgroundView = [[UIView alloc] init]; backgroundView.backgroundColor = [UIColor greenColor]; cell.backgroundView = backgroundView;

右边指示器显示,比如箭头、打勾,也可以自定义图片等等

// 显示箭头
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// 自定义显示,如开关Switch
    cell.accessoryView = [[UISwitch alloc] init];

 设置tableview 不能滚动

 self.tableView.scrollEnabled =NO; 
原文地址:https://www.cnblogs.com/xsphehe/p/5614504.html