进击的UI-----------------UITableView(表视图)

tintColor 标题色
barTintColor 背景色
1.UITableView表视图
1⃣️:Plain:self.tableView = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
2⃣️:Grouped:self.tableView = [[UITableView alloc]initWithFrame:self.frame style:UITableViewStyleGrouped];
3⃣️:方法:
①:设置行高:self.tableView.rowHeight = 100;
②:分割线颜色: self.tableView.separatorColor = [UIColor magentaColor];
③:分割线样式:self.tableView.separatorStyle =  UITableViewCellSeparatorStyleSingleLine;
2.表视图的重用机制
代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    NSLog(@"Cell");
//    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle   reuseIdentifier:nil];
    // 1.重用标识符
    static NSString *cell_id = @"cell";
    // 2.去重用池寻找有此标识符的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id];
    // 3.做判断
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cell_id];
    }
    // 注意:重用池是由一个NSMutableSet实现的,iOS为了保证内存的合理使用,提供了重用机制,划出屏幕的cell 会放到重用池,加上一个重用标识符,如果下次使用,直接使用代标识符的cell即可
    cell.textLabel.text = @"BOOM";
    cell.detailTextLabel.text = @"ShaKaLaKa";
    return cell;
}
3.UITabelViewDelegate
①:引入代理:@interface RootViewController ()<UITableViewDataSource>
②:设置代理: self.rv.tableView.dataSource = self;
③:调用方法:
// 告诉tabelView有多少分组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    NSLog(@"section");
    return 3;
}
// 告诉tabelView有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSLog(@"row");
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 2;
    }else
    // row 表示tabelView的行(有多少条Cell) Section表示有多少节(多少个分组)
    return 1;
}
// 告诉tabelView Cell 是什么样式的
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //
    NSLog(@"Cell");
    // 初始一个Cell 样式
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    // 设置图片
    cell.imageView.image = [UIImage imageNamed:@"1.png"];
    // 设置文本
    cell.textLabel.text = @"BOOMShaKaLaKa";
    // 索引值包括row和section
    cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.section];
    // indexPath表示每一条Cell的路径,每一条Cell的路径可以通过他所在的分组,和他所在分组的哪一行来确定
    if (indexPath.row == 1) {
        cell.textLabel.text = @"123";
    }
    // 设置详情的文本
    cell.detailTextLabel.text = @"BangBangBang";
    // 附加的View 放控件
    cell.accessoryView = [[UISwitch alloc]init];
//    cell.accessoryType =
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    return cell;
}
4.UITAbleViewDataSource
①:引入代理:@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>
②:设置代理:self.rootRV.tableView.delegate = self;
③:调用方法:
//  UITableViewDataSource,UITableViewDelegate 方法方法
// 设置分区头部的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [NSString stringWithFormat:@"联系人"];
}
// 设置分区尾部的标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    return [NSString stringWithFormat:@"lalala"];
}
// 显示右侧竖排索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    NSArray *arr = @[@"0",@"1",@"2"];
    return arr;
}
// 返回header的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 100;
}
// 返回header的View
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    view.backgroundColor = [UIColor cyanColor];
    return view;
}
// 返回尾部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 20;
}
// 返回尾部的VIew
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *temp = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    temp.backgroundColor = [UIColor blackColor];
    return temp;
}
// 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 2 && indexPath.row == 2) {
        return 100;
    }else
    return 44;
}
// 跳转 选中某一行 千万别选错方法 didDeselectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    if (indexPath.row == 2 && indexPath.section == 2) {
//        NSLog(@"hahaha");
//    }
    NextViewController *next = [[NextViewController alloc]init];
//    [self.navigationController pushViewController:next animated:YES];
    // 模态 出现
    [self.navigationController presentViewController:next animated:YES completion:^{ }];
  
}
- (void)buttonAction:(UIButton *)sender{
    [self dismissViewControllerAnimated:YES completion:^{
       
    }];
}
 
原文地址:https://www.cnblogs.com/sharkHZ/p/4984116.html