UITableView

430 阅读1分钟

 创建UITableView:

- (void)layoutUI {
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:(UITableViewStylePlain)];
    tableView.estimatedSectionHeaderHeight = 0;
    tableView.estimatedSectionFooterHeight = 0;
    tableView.rowHeight = 50;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.tableFooterView = [UIView new];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableView];
}

#pragma mark - tableView Delegate & DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 官方建议使用以下方法,此方法要求必须预先注册可重用单元格,否则崩溃!
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    //一旦在注册了可重用Cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        
        CALayer *lay = [[CALayer alloc] init];
        lay.frame = CGRectMake(0, 49, self.view.frame.size.width, 1);
        lay.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1].CGColor;
        [cell.contentView.layer addSublayer:lay];
    }
    cell.textLabel.text = [_dataArr[indexPath.row] valueForKey:@"title"];
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    Class cls = NSClassFromString([self.dataArr[indexPath.row] valueForKey:@"vc"]);
    UIViewController *vc = [[cls alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

UITableViewCell注册:

//1.纯代码自定义的cell注册如下:(需要重写initWithStyle)
[self.tableView registerClass:[ZJBaiduMapCell class] forCellReuseIdentifier:ID];
//2. 使用Xib自定义的cell,注册如下
[self.tableView registerNib:[UINib nibWithNibName:@"ZJBaiduMapCell" bundle:nil] forCellReuseIdentifier:ID];

dequeueReusableCellWithIdentifier:和dequeueReusableCellWithIdentifier:forIndexPath:的比较:

  • 前者当没有可用cell,会返回nil;而后者会直接崩溃;
  • 前者需要手动检查cell是否为空,而后者不需要;

注:注册cell要仔细

//显示每组头标题名称-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{}
//显示每组尾标题名称-(NSString*)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{}
//显示每组有多少行- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {}
//显示每组的行高-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{}
//显示内容- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{} 
//左滑删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {}
//对应的行是否可以被编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {}