美文网首页
UITableView 基础使用

UITableView 基础使用

作者: 奇梦人 | 来源:发表于2020-04-04 21:19 被阅读0次
    1. 初始化
    - (void)viewDidLoad {
        [super viewDidLoad];
     
        //style  UITableViewStylePlain 规范的列表视图
        //style  UITableViewStyleGrouped 分组列表视图
        UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        tableView.dataSource = self;
        tableView.delegate = self;
        [self.view addSubview: tableView];
        
    }
    
    1. 遵守相关协议
      // UITableViewDelegate 设置具体行高度 头尾视图 实现协议
    @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
    
    @end
    
    1. 在 ViewController 实现相关的方法
    
    //设置表格视图有多少行
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return 10;
    }
    
    // 设置有几组
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 2;
    }
    
    
    //设置 cell 视图
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
       
        UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"id"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"id"];
        }
        
        cell.textLabel.text = @"主标题";
        cell.detailTextLabel.text = @"副标题";
        cell.imageView.image =[UIImage imageNamed:@"loginbackground"];
        return cell;
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    // 设置行高
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        if(indexPath.section == 0){
            return 100;
        }else{
            return 40;
        }
    }
    
    // 分区头视图高度
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
        return 50;
    }
    
    //分区尾视图高度
    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
        return 50;
    }
    
    //设置分区的头视图
    - (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,50)];
        view.backgroundColor=[UIColor greenColor];
        return view;
    }   // custom view for header. will be adjusted to default or specified header height
    
    //设置分区的尾视图
    - (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,50)];
        view.backgroundColor=[UIColor blueColor];
        return view;
    }   // custom view for footer. will be adjusted to default or specified footer height
    
    
    1. UITableView 的交互行为

    cell 点击

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        NSLog(@"这是第 %@",indexPath);
    }
    
    image.png
    1. 设置 tableview 可编辑
    tableview.editing  =  YES;
    
    // 需要实现这个方法来设置 cell 的编辑模式
    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
        if(indexPath.section == 0){
            return UITableViewCellEditingStyleDelete; // 删除风格
        }else{
            return UITableViewCellEditingStyleInsert; // 新增风格
            UITableViewCellEditingStyleNone;
        }
        
    }
    
    image.png

    点击删除和新增的回调方法

    
    //删除调用
    - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
        
        return @"删除";
    }
    
    //新增调用
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
        
    }
    
    1. 为 TableView 添加索引

    这个方法需要返回一个数组 ,这个数组必须要对应 UITableView 控制的分区一致,并且他们之间有一一对应关系

    - (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
        
        return @[@"1",@"2"];
    }
    
    image.png

    相关文章

      网友评论

          本文标题:UITableView 基础使用

          本文链接:https://www.haomeiwen.com/subject/occsphtx.html