OC控件--UITableView

设计思想:

通过代理的方法对它进行回调设置

UITableView有两种风格:

UITableViewStylePlain   默认平铺的风格

UITableViewStyleGrouped  默认分组的风格

在初始化控件的时候可以设置,如下:

UITableView *tableview=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];

UITableView简单使用步骤:

1.设置代理和数据源

2.设置分区数

3.设置分区行数

4.对行的内容进行设置

数据源的必要性:UITableView的数据都从数据源中读取 

基本上这样下来,就可以简单的看到一个UITableView视图.

其他一些设置:

1.头尾视图

先设置头尾视图,再设置高度;

用到的函数:

//设置尾视图
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{;
//设置尾视图高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
//设置头视图
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
//设置头视图高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

2.编辑属性

主要用到的两个函数:

//设置tableview编辑风格
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
//编辑状态下,点击功能按钮触发的方法
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;

一般练习使用的时候,可以先设置一个按钮来触发编辑属性,具体实现代码在后面可以看到

3.索引属性

//加入索引
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView;
//索引分栏,使用时要去掉头尾视图
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

整个的电话本就写好了,一些数据为了方便写了死数据,代码如下:



#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>//代理
{
    //数据源
    NSArray *_dataArray;
    UITableView *tableview;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化数据源
    _dataArray=@[@"阿姨",@"安安",@"阿姊",@"b妍希",@"b清乐",@"b清越",@"蔡文姬",@"曹操",@"查理",@"段誉",@"段逸",@"段柒",@"e苏苏",@"e茶茶",@"晚晚",@"傅心汉",@"傅沉",@"傅斯年",@"郭襄",@"郭静",@"耿耿",@"合照",@"何以琛",@"黄继光"];
    // Do any additional setup after loading the view.
    /*
     UITableViewStylePlain   默认平铺的风格
     UITableViewStyleGrouped  默认分组的风格
     */
    tableview=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-30) style:UITableViewStyleGrouped];
    UIButton * btn=[[UIButton alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-30, self.view.frame.size.width, 30) ];
    btn.backgroundColor=[UIColor yellowColor];
    [btn setTitle:@"编辑" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [self.view addSubview:btn];
    //设置代理和数据源
    tableview.delegate=self;
    tableview.dataSource=self;
    [self.view addSubview:tableview];
}
-(void)click:(UIButton *)btn{
    //通过按钮的改变tableview的编辑模式
    if(tableview.isEditing){
        tableview.editing=NO;
       [ btn setTitle:@"编辑" forState:UIControlStateNormal];
    }else{
        tableview.editing=YES;
        [btn setTitle:@"完成" forState:UIControlStateNormal];
    }
}
//分区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 8;
}
//设置每个分区的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return 3;
            break;
        case 1:
            return 3;
            break;
        default:
            return 3;
    }
}
//设置行的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //分取第一个分区
    if(indexPath==0){
        return 60;
    }else{
        //获取第一个分区第一行
        if(indexPath.row==0){
            return 44;
        }else{
            return 33;
        }
    }
}
//对tablview中的行进行设置
//每一次滑动都要造成内存开辟
//-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    UITableViewCell *cell=[[UITableViewCell alloc]init];
//    cell.textLabel.text=[NSString stringWithFormat:@"%d",indexPath.row];
//    return cell;
//}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cellId"];
    if(cell==nil){
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellId" ];
    }
    cell.textLabel.text=_dataArray[indexPath.section*3+indexPath.row];
    //加个图片
    UIImage *image1=[UIImage imageNamed:@"2.jpg"];
    //载体
    UIImageView *imageview=[[UIImageView alloc]initWithImage:image1];
    imageview.frame=CGRectMake(0, 0, 20, 30);
    imageview.backgroundColor=[UIColor yellowColor];
    [cell.contentView addSubview:imageview];
    return cell;
    
}
#if 0
//设置尾视图
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 50) ];
    view.backgroundColor=[UIColor grayColor];
    return view;
}
//设置尾视图高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 60;
    
}
//设置头视图
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 80) ];
    view.backgroundColor=[UIColor yellowColor];
    return view;
}
//设置头视图高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}
#endif
//设置tableview编辑风格
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section==0){
        return UITableViewCellEditingStyleInsert;
    }else{
        return UITableViewCellEditingStyleDelete;
    }
}
//编辑状态下,点击功能按钮触发的方法
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section==0)
    {
        NSLog(@"tableview insert 触发数据处理");
    }else{
        NSLog(@"tableview delete 触发数据处理");
        
    }
}
//索引栏
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return @[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H"];
}
//首部索引,使用时要去掉头尾视图
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
   return [NSString stringWithFormat:@"%c",section+'A'];
}
@end

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值