ios UITableView

  目录

  一、分为两种

  二、显示数据步骤;

         1、设置协议

         2、实现三个方法

         3、设置行高

         4、常见属性        

             1、 设置点击选中样式颜色

             2、设置常用自定义Cell; 

         5、UITableViewCell重用 

         6、设置右侧title

         7、实现的多个方法汇总

         8、刷新TableView数据   

         9、滚动TableView到指定位置(可以滚动到底部)

        10、自定义侧滑删除和侧滑多个按钮

        11、设置编辑模式

             

    一、分为两种

         UITableView

    二、显示数据步骤:

         1、设置协议

 self.tableView.dataSource=self;//设置协议
 interfaceViewController ()<UITableViewDataSource>//

 

      2、实现三个方法

  

//设置TablerView显示几组数据,默认分一组;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 4;
}
//设置UITabView每组显示几行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 7;
}
//设置每一行的每一组显示单元格的什么内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    //创建一个单元格并返回
    UITableViewCell *tableViewSell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    
        tableViewSell.textLabel.text= @"nihao";
        tableViewSell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;//显示右边箭头
        tableViewSell.accessoryView=[[UISwitch alloc]init];//自定义右边显示控件
    
    
    return tableViewSell;
}

- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    return @"标题";
}

- (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    return @"底部描述";
}

     

  

         3.设置行高

     //如果行高一致设置行高
    self.tableView.rowHeight=120;
//对于每行的行高不一致的情况,无法通过rowHeight来设置,此时只能通过代理来设置 self.tableView.delegate=self;//1.添加代理 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>//2.实现代理 - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if(indexPath.row%2){ return 60; }else{ return 120; } }

       

  

        4.常见属性              

      UITableViewCell常见属性:
     *imageView
     *textLable
     *detailTextLable
 
     accessoryType//右侧常用按钮
     accessoryView//右侧自定义按钮
     backgroundColor//设置单元格背景颜色;
     backgroundView//设置单元格背景控件:可以是控件
     //设置选中的单元格的背景颜色
     UIView *bgView=[[UIView alloc]init];
     bgView.backgroundColor=[UIColor greenColor];
     tableViewSell.selectedBackgroundView=bgView;

     //设置选中样式颜色,不显示选中颜色

      cell.selectionStyle = UITableViewCellSelectionStyleNone;

         

     UITableView常见属性
     self.tableView.rowHeight=120;// 设置行高
     self.tableView.separatorColor;//分割线的颜色
     self.tableView.separatorStyle=UITableViewCellSelectionStyleNone;//分割线的样式;不显示分割线
     self.tableView.tableHeaderView=[UIButton buttonWithType:UIButtonTypeContactAdd];//一般放广告
     self.tableView.tableFooterView=[UIButton buttonWithType:UIButtonTypeContactAdd];//一般放加载更多

 

        5.UITableViewCell重用:

   

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    Groups *group=self.groups[indexPath.section];
    Car *car=group.cars[indexPath.row];
    static NSString *ID=@"car_cell";
    
    UITableViewCell *tableCell=  [tableView dequeueReusableCellWithIdentifier:ID];
    
    if(tableCell==nil){
        tableCell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    tableCell.textLabel.text=car.name;
    tableCell.imageView.image=[UIImage imageNamed:car.icon];
    return tableCell;
}

 

           6.设置右侧title

//设置右侧title
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return [self.groups valueForKeyPath:@"title"];
}

             

 

         

       7.实现的多个方法汇总

//设置组数
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return self.groups.count;
}
//设置组名
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
     Groups *groups=  self.groups[section];
    
    return groups.title;
}
//设置每组的条目数
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    Groups *groups=  self.groups[section];
    return  groups.cars.count;
}
//设置右侧title
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    
    return [self.groups valueForKeyPath:@"title"];
}
//返回单元格
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    Groups *group=self.groups[indexPath.section];
    Car *car=group.cars[indexPath.row];
    static NSString *ID=@"car_cell";
    
    UITableViewCell *tableCell=  [tableView dequeueReusableCellWithIdentifier:ID];
    
    if(tableCell==nil){
        tableCell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    tableCell.textLabel.text=car.name;
    tableCell.imageView.image=[UIImage imageNamed:car.icon];
    return tableCell;
}

 

      8.刷新TableView数据   

    //刷新所有
    [self.tableView reloadData];
    
    //刷新指定的组
    NSIndexSet  *nsIndexSet= [NSIndexSet indexSetWithIndex:1];
    [self.tableView reloadSections:nsIndexSet withRowAnimation:UITableViewRowAnimationAutomatic];
    
    //刷新指定的行  ,组和行参数
    NSIndexPath *idxPath=[NSIndexPath indexPathForRow:1 inSection:1];//几列几组
    [self.tableView reloadRowsAtIndexPaths:@[idxPath] withRowAnimation:UITableViewRowAnimationAutomatic];
// 删除当前indexPath

     [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];

   

    9.滚动TableView到指定位置(可以滚动到底部)

   //把tableview滚动到最底部
      NSIndexPath *idxPath=[NSIndexPath indexPathForRow:1 inSection:0];//几列几组
    [self.tableView scrollToRowAtIndexPath:idxPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
  

 

 

    10.自定义侧滑删除和侧滑多个按钮

//重写这个方法才能实现左滑删除功能,出现一个按钮
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.dataArray removeObjectAtIndex:indexPath.row];
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];

    //[self.tableView reloadData];//刷新
}

//重写这个方法才能实现多个按钮点击,如果实现这个方法,commitEditingStyle将无效
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"关注" handler:^(UITableViewRowAction *_Nonnull action, NSIndexPath *_Nonnull indexPath) {
        NSLog(@"关注");
    }];
    action.backgroundColor = [UIColor grayColor];
    UITableViewRowAction *actionDelete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *_Nonnull action, NSIndexPath *_Nonnull indexPath) {
        NSLog(@"删除");
    }];
    actionDelete.backgroundColor = [UIColor redColor];
    UITableViewRowAction *actionDefault = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"默认" handler:^(UITableViewRowAction *_Nonnull action, NSIndexPath *_Nonnull indexPath) {
        NSLog(@"默认");
    }];
    actionDefault.backgroundColor = [UIColor greenColor];
    return @[action, actionDelete, actionDefault];
}

//左滑删除文字定义
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除数据";
}

 

 

  11、设置编辑模式

    //设置编辑模式
    self.tableView.editing = YES;
    //设置编辑模式动画

      [self.tableView setEditing:YES animated:YES];

 

  

     注:

        //数据赋值
[self setValuesForKeysWithDictionary:dict]; NSMutableArray
*arrayM=[NSMutableArray array]; for (NSDictionary *dictCar in dict[@"cars"]) { [arrayM addObject: [[Car alloc] initWithCar:dictCar]]; } self.cars=arrayM;

 

posted @ 2020-04-17 14:55  张亚楠  阅读(906)  评论(0编辑  收藏  举报