UITableView 的使用总结

确定单元格的位置:首先要知道分区号,在知道行号。
UITableView:API文档的总结:
1、UITableView的父类是:UIScrollview,所以他是能滚动的,但是只能在竖直方向滚动。
2、UITableView是iOS中提供的用来以列表的形式展示数据的方法,但是只有一列。
3、UITableView可以由多个分组构成(section),每个分组下可以由很多行(row),分组和行的下标都是从0开始的(例如:section班级分组;row同学所在的排数)
4、UITableView可以有两种样式(Plain)和(Grouped)样式,一旦给tableview指定了样式之后就不能修改了。
5、UITableView很多方法都和NSIndexPath有关,NSIndexPath中存储的是当前你要使用的单元格(cell)所在分区的下标和所在分区中行的下标


创建UITableView对象,并且对UITableView对象配置属性
/    1、创建控件对象
    UITableView *tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
//    2、配置属性
//    2.1、配置单元格(cell)的行高
    tableView.rowHeight = 100;
//    2.2、配置单元格分割线的颜色
    tableView.separatorColor = [UIColor clearColor];
//    2.3、配置单元格分割线的样式
    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
//    2.4、配置tableView的表头,表头很重要,经常用来做轮播图。
//     2.4.1、准备一个表头视图
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
    headerView.backgroundColor = [UIColor greenColor];
//     2.4.2、将准备好的表头视图赋值给tableView的表头视图属性
    tableView.tableHeaderView = headerView;
    [headerView release];
//    2.5、配置tableView的表尾
    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
    footView.backgroundColor = [UIColor redColor];
    tableView.tableFooterView = footView;
    [footView release];
//        快速去除多余的(没有使用的)单元格
tableView.tableFooterView = [[[UIView alloc]init]autorelease];
//    2.6、配置数据源代理
    tableView.dataSource = self;
//    2.7、配置业务代理
    tableView.delegate = self;

 


创建单元格,并为单元格配置数据所调用的方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
cell(单元格):
  UITableViewCell:单元格视图,继承自UIView。用来展示数据 
   cell上有两个视图,一个是contentView,一个是accessoryView(辅助视图),而我们用到的cell的属性textLable,detailTextLabel,imageView这些视图都是在contentView上,所以当我们自定义cell的时候,一定要把自定义的控件放到contentView上;
给cell添加属性:
1.给cell的imageView属性赋值
    cell.imageView.image = [UIImage imageNamed:@"2"];
//    2.给cell的textLable附上文本内容。
    cell.textLabel.text = [NSString stringWithFormat:@"%ld--%ld",indexPath.section, indexPath.row];
//    3.给cell的detailTextLable附上文本内容
    cell.detailTextLabel.text = @"中岛美嘉";
//    4、设置cell右边界的辅助视图
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//    cell.accessoryView = [[[UISwitch alloc]init]autorelease];
//        自定义辅助视图
    UIView *accessoryView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, cell.frame.size.height)];
    accessoryView.backgroundColor = [UIColor redColor];
    cell.accessoryView = accessoryView;
    [accessoryView release];
cell(单元格)的重用:
cell的重用是为了避免反复的消耗系统资源,还能达到节省内存的作用
//    cell重用的步骤:
//    1、创建一重用标识(重用ID),这个标识在cell创建的时候使用,也在重用池中取cell是判断使用;reuseIdentifier(重用标识 )
//    使用static修饰,重用标识的字符串变量,只被初始化一次,提高一下重用的效率。
   static NSString *identfier = @"cell";
//    2、当需要一个单元格时(cell),先去重用池中,根据重用ID去查找有没有可以使用的单元格。
//    如果有:直接从重用池中拿来使用(修改这个单元格上的数据)。
//    如果没有:此时只能直接新建一个新的单元格。
//    tableView:就是当前代理的委托人
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identfier];
//    3、判断是否成功取到可重用的cell
    if (cell == nil) {
        NSLog(@"==========");
        cell = [[[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:identfier]autorelease];
    }
   



     从数组中读取数据,并且显示在tableview上。
     需要在实现业务代理的方法中添加页眉、页脚和右边框的索引
第一步:定义一个大的数组用来存放其他的小的数组
 NSArray *lArr = @[@"李萌",@"李武华",@"李白",@"李玉怀"];
 NSArray *zArr = @[@"张波",@"张长永",@"张三",@"张子乾"];
 NSArray *wArr = @[@"王向凯",@"王维",@"王忠伟"];
self.bigArray = @[lArr,zArr,wArr];(此时的大的数组是全局变量)
第二步:让视图tableview遵循数据源代理,并且通过数据源代理方法能够实现
          1、 配置tableview的分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  // 根据数组元素的个数,确定分区的个数
    return self.bigArray.count;
}    2、 返回每个分区对应的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    根据分区的下标找出大数组中小数组中的元素个数
    return [self.bigArray[section] count];
}
第三步:在创建单元格,并且能够为单元格配置属性的方法中实现
#pragma mark 从数组中取出数据展示:
    1、取出大数组中的元素
    NSArray *nameArray = self.bigArray[indexPath.section];
    2、从nameArray中取出名字
    NSString *name = nameArray[indexPath.row];
    3、将我们取出的值赋值给
    cell.textLabel.text = name;
    cell.imageView.image = [UIImage imageNamed:@"2"];
   



     从字典中读取数据,并且显示在tableview上面,
      需要在实现业务代理的方法中添加页眉、页脚和右边框的索引
第一步:创建一个字典存放数组:此时的字典也是全局变量。同时定义一个数组(orderKey)用来存放字典中的所有key值
    NSArray *lArr = @[@"李萌",@"李武华",@"李白",@"李玉怀"];
  NSArray *zArr = @[@"张波",@"张长永",@"张三",@"张子乾"];
  NSArray *wArr = @[@"王向凯",@"王维",@"王忠伟"];
   self.dictionary = @{@"L":lArr,@"Z":zArr,@"W":wArr};

第二步:取出字典中的key值
   NSArray *keys= self.dictionary.allKeys;
第三步:将排好序的key值数组赋值到数组orderKey中
   self.orderKey = [keys sortedArrayUsingSelector:@selector(compare:)];
第四步:让视图tableview遵循数据源代理,可以得到:
    1、配置tableview的分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//    根据字典中键值对个数,确定分区的个数
     return self.orderKey.count;
//    return self.dictionary.count;
}
    2、 返回每个分区对应的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    1.根据分区的下标取出self.orderKey数组中存放的key值
    NSString *key = self.orderKey[section];
//    2.使用key值取出字典中的value值(数组)并取出数组中的元素个数
//    [self.dictionary objectForKey:key]
    return  [self.dictionary[key] count];
}
第五步:在创建单元格,并且能够为单元格配置属性的方法中实现
#pragma mark 从字典中取数据显示
//    1.根据分区下标取出self.orderKey中的元素(key值);
    NSString *key = self.orderKey[indexPath.section];
//    2.取出key值在字典中对应的数组
    NSArray *name = self.dictionary[key];
//    3.根据行下标取出数组中的名字并复制给textLabel.text;
    cell.textLabel.text = name[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:@"2"];
第六步:配置分区的右侧的分区索引,区头的顺序和数组中的元素的顺序一致
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
//根据排好序的数组设置右侧分区索引
    return self.orderKey;
}
配置每个分区的页眉:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//    if (0 == section) {
//        return @"L";
//    }else if (1 == section){
//    return @"Z";
//    }else{
//   
//    return @"W";
//    }
//    根据排好序的数组返回区头
    return self.orderKey[section];
}
配置分区的右侧的分区索引,区头的顺序和数组中的元素的顺序一致
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
//根据排好序的数组设置右侧分区索引
    return self.orderKey;
}
配置每个分区的页脚(区尾)title
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    return @[@"f",@"s",@"l"][section];
}
    tableView的业务代理方法,并且实现页面的跳转。并且通过属性进行传值。
#pragma mark tableView的业务代理方法
//返回cell的行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (0 != indexPath.row % 2) {
        return 80.0;
    }else{
        return 160.0;}
}

//触发时机:点击cell的时候会触发的方法,所以要完成点击cell跳转到其他界面,就把方法的实现写在这个方法里面。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    NSLog(@"%ld--%ld",indexPath.section,indexPath.row);
    DetailViewController *detailVC = [[DetailViewController alloc]init];
  
    detailVC.textString = [NSString stringWithFormat:@"%ld--%ld",indexPath.section,indexPath.row];
  
    [self.navigationController pushViewController:detailVC animated:YES];
    [detailVC release];
  
}
//触发时机:点击一个cell之后,再点击其他的cell时触发,
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0){

NSLog(@"%ld--%ld",indexPath.section,indexPath.row);

}

转载于:https://www.cnblogs.com/zzu-xll/p/5082853.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值