码迷,mamicode.com
首页 > 其他好文 > 详细

UITableView

时间:2016-05-05 22:18:19      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:

- (void)viewDidLoad {

    [super viewDidLoad];

    

    [self createData];

    

    //tableview样式 UITableViewStylePlain 线性的

//    UITableViewStyleGrouped 组的样式

    

    UITableView * tableView =  [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];

    

    tableView.rowHeight = 100;//设置行高

   

    //分割线样式 UITableViewCellSeparatorStyleSingleLine 单线

//    UITableViewCellSeparatorStyleNone 无线

    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;//无分割线

    

    //线的颜色

    tableView.separatorColor  = [UIColor redColor];

    

    //设置分割线距离原来位置的 偏移量         

    tableView.separatorInset = UIEdgeInsetsMake(0, 200, 0, 50);

    

    UIView *  blueView = [[UIView alloc] initWithFrame:tableView.bounds];

    

    blueView.backgroundColor = [UIColor blueColor];

    

    //设置背景视图

//    tableView.backgroundView = blueView;

    

    //设置tableView的头视图

    tableView.tableHeaderView = blueView;

    

    UIView * footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

    footView.backgroundColor = [UIColor yellowColor];

    //设置tableView  的尾巴视图

    tableView.tableFooterView = footView;

    //背景颜色

//    tableView.backgroundColor = [UIColor redColor];

    

    //添加数据

    tableView.dataSource = self;

   

    //视图相关的代理方法

    tableView.delegate = self;

    

    //添加视图

    [self.view addSubview:tableView];

    

    

    // Do any additional setup after loading the view, typically from a nib.

}

 

 

#pragma mark tableViewDataDelegate  数据源的代理

 

//返回 每组有多少条数据

//返回 每组有多少行

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    //section

    if (section==0) {//如果是第一组

        return 2;//

    }

    return _dataSource.count;

    

}

//返回有多少个分区 分组

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    

    return 2;

    

}

 

//返回 对应位置的cell

 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    

  

    //创建一个复用标识

    static  NSString * cellIndentifier = @"celltypeone";

    

    //通过复用标识 tableview中可以复用的cell

    

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];

    

    if (cell == nil) {//如果没有找到可复用的cell

//        UITableViewCellStyleDefault 默认样式

//        UITableViewCellStyleSubtitle 带详细label的样式

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIndentifier];//通过复用标志创建cell

        static  int count = 0;

        

        NSLog(@"创建了几次%d",++count);

        

        //UITableViewCellAccessoryDisclosureIndicator 向右箭头

        //UITableViewCellAccessoryCheckmark 对勾

        //UITableViewCellAccessoryDetailDisclosureButton 感叹号 加上箭头

        //UITableViewCellAccessoryDetailButton 感叹号

//        cell.accessoryType = UITableViewCellAccessoryDetailButton;//设置cell附件视图样式

        

        //自定义附件视图

//        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];

//        view.backgroundColor = [UIColor redColor];

//        

//        cell.accessoryView = view;

        

        //设置选中的样式 

        cell.selectionStyle = UITableViewCellSelectionStyleBlue;

        

    }

    

    //修改cell的内容

    //indexPath.row 第几行

    //indexPath.section 第几组

 

    //设置文字

    

    cell.textLabel.text = [NSString stringWithFormat:@"我是第%ld,%ld",indexPath.section,indexPath.row];

    //设置图片

    cell.imageView.image = [UIImage imageNamed:_dataSource[indexPath.row]];

    

    //UITableViewCellStyleSubtitle 样式下才会有

    cell.detailTextLabel.text = @"我是详细信息";

    

    NSLog(@"什么时候调呢? cell 将要显示的时候都会调用");

    

    return cell;

    

}

 

 

#pragma mark  tableViewDelegate 视图相关的代理方法

 

//返回 对应位置的行高

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    //1

    if (indexPath.section == 0) {

        return  200;

        

    }else{

        

        if (indexPath.row == 2) {

            return 300;

        }else{

            return 100;

        }

        

    }

}

//返回对应组的 头视图

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    

    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 80)];

    label.backgroundColor = [UIColor purpleColor];

    

    label.text = [NSString stringWithFormat:@"我是第%ld组的头",section];

    

    return label;

}

//返回对应组的  尾巴视图

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

   

    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 80)];//宽度高度无效的

    label.backgroundColor = [UIColor greenColor];

    

    label.text = [NSString stringWithFormat:@"我是第%ld组的尾巴",section];

    

    return label;

}

 

//返回对应组 头的高度

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    

    if (section == 0) {

        return 100;

    }else{

        return  50;

    }

    

}

 

//返回对应组 尾巴的高度

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    

    return 40;

    

}

 

//选中某一行

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"选中了第%ld组,第%ld",indexPath.section,indexPath.row);

    

    

}

 

//取消选中

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

    NSLog(@"取消选中了第%ld组,第%ld",indexPath.section,indexPath.row);

 

    

}

 

 

5.UITableViewController

 

【实验】

1.使用单组UITableView显示数据

 

2.使用分组的UITableView显示分组的数据

 

3.UITableView的样式制定(使用官方的Cell进行定制)

【注】tableView的定制可以分为两种方式

<1>通过tableView的属性进行定制

<2>通过协议方法定制

 

4.UITableView的cell编辑

(1)删除cell

(2)添加cell

(3)移动cell

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

    

    //返回是否可以编辑 该行

    if (indexPath.row == 0) {

        return NO;//第一行不可编辑

    }

    

    return YES;

    

}

 

//返回是否可以移动某行//?????

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{

    

    if (indexPath.row == 4) {

        return NO;

    }

    return YES;//是否可以移动某indexPath

}

 

// 某个位置sourceIndexPath 移到 目标行destinationIndexPath

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{

    

    if (sourceIndexPath.section == destinationIndexPath.section) {//如果是同一组

        NSMutableArray * datas =  _dataSource[sourceIndexPath.section];

       

        //交换元素位置

        [datas exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];

        

    }else {

        

        //不是同一组

        NSMutableArray * destinationArray = _dataSource[destinationIndexPath.section];

       

        NSMutableArray * sourceArray = _dataSource[sourceIndexPath.section];

        

        NSString * str = sourceArray[sourceIndexPath.row];//取的原位置对象

       

        [destinationArray insertObject:str atIndex:destinationIndexPath.row];//将对象插入到目标位置

        

        [sourceArray removeObjectAtIndex:sourceIndexPath.row];//源数组 移除原位置数据

               

    }    

}

 

//点击了插入 或者 删除按钮的时候  会被调用

 

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    

    if (editingStyle == UITableViewCellEditingStyleInsert) {

        //点击了插入

        NSLog(@"插入");

        

        NSMutableArray * datas = _dataSource[indexPath.section];

        [datas insertObject:@"??????" atIndex:indexPath.row];//修改数据源

        

        

        //indexPath位置插入一行视图

        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

        

        

    }else if (editingStyle == UITableViewCellEditingStyleDelete){

        //点击了 删除

        NSLog(@"删除");

        

        //找到对应的数据数组

        NSMutableArray * datas = _dataSource[indexPath.section];

        [datas removeObjectAtIndex:indexPath.row];

        

        //更新视图前一定要更新数据源

        

        //删除一行视图

        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];

            

    }   

    

}

 

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

//    UITableViewCellEditingStyleNone,

//    UITableViewCellEditingStyleDelete, 删除

//    UITableViewCellEditingStyleInsert   插入

    //当进入编辑状态时返回 对应行的编辑样式

    

    if (indexPath.row ==3) {

    return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;//多选状态?????

        

    }

    

    if (indexPath.row == 2) {

        return UITableViewCellEditingStyleInsert;

    }else{

        return UITableViewCellEditingStyleDelete;

    }

    

}

 

 

#pragma mark  delegate  的方法

 

//设置对应行的删除按钮文字

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0){

    

    if (indexPath.row == 8){

        return @"我是第八行";

    }

    

    return @"??";

}

 

 

 

5.实现搜索栏功能

 

 _tableView.sectionIndexBackgroundColor = [UIColor clearColor];//设置 搜索指示栏 背景颜色

    _tableView.sectionIndexTrackingBackgroundColor = [UIColor redColor];//设置摸到 搜索指示栏 背景颜色

    

    _tableView.sectionIndexColor = [UIColor blackColor];//设置 搜索指示栏 文字颜色

 

//返回跳转到哪个section点击搜索栏跳到对应的数组

 

-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{

    

    //title选中这个 指示的title   index 选中的指示的位置

    

    

    if ([title isEqualToString:@"同学"]) {

        return 3;

    }

    

    

    return index;

}

 

//返回 组指示 数组//创建指示组

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

    

    return [self titles];

    

}

UITableView

标签:

原文地址:http://www.cnblogs.com/tony0571/p/5463298.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!