• cell的各种使用和赋值 总结


    cell可以分为:自定义cell,系统的cell ,cell的自适应,.xib的cell

    //第一种cell:系统cell

    在 UIViewController下创建UITableView

    //1.0 UITableView的父类是UIScollView,所以他可以滚动,但是支持数字方向的滚动
         2.UITableView是以列的形式展示数据但是只要一列
         3.UITableView可以有0个活多个分区(section)构成,每一个分区可以与很多的行(row)且通过UITableView中分区下标区分是哪一个分区,row是根据所在分区中的下标来区分section和row的下标都是从0开始
         4.UITableView可以有两种样式,plain和group,创建的时候必须指定一个样式,且指定样式之后就不能修改
         5.UITableView的很多方法的返回值和参数都有NSIndexPath类对象,NSIndexPath对象存储的是选中单元格(cell)的所在分区下标以及分区中行的下标

    //2.1设置单元格的高度
        tableView.rowHeight = 142;
        //2.2设置分割线的颜色
        tableView.separatorColor  = [UIColor greenColor];
        //2.3设置分割样式
        tableView.separatorStyle =UITableViewCellSeparatorStyleSingleLine;

     //2.4设置表头视图 一般在表头视图上放置轮播图,

    //2.5设置表尾视图(小技巧:可以使用表尾视图收起虚假的cell样式)

    //2.6VIPtableview的数据源代理
        tableView.dataSource = self;
    //    tableView.delegate = self;

    //必须实现的代理(1--2)

    //1 .返回UITableView中section(分区个数)

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

    //2 .返回每个分区中cell的个数

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

    //返回分区索引栏
    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;

    //返回每个分区的区尾上的标题
    - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

    //返回每个分区的页眉,区头上的标题
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;

    //创建cell对象并将cell对象返回,而且还能在这个方法中设置cell要显示的数据
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    //系统cell

    UITableViewCell *cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil]autorelease];

    // indexPath.section 存储的分区下标
    //indexPath.row 存储行所在分区的下标

    //给cell上的textlabel赋值
        cell.textLabel.text = [NSString stringWithFormat:@"%ld- %ld",indexPath.row,indexPath.section];
        //给cell上imageView的赋值
        cell.imageView.image = [UIImage imageNamed:@"blu"];
        //给cell上detailTextLable
        cell.detailTextLabel.text = @"详情信息";
        //设置cell上的辅助视图样式
        //cell.accessoryType = UITableViewCellAccessoryCheckmark;
        //放置UIControl控件
        //cell.accessoryView = [[[UISwitch alloc]init]autorelease];

    cell的重用机制

    //用static修饰的变量存放在静态区,方法执行完也不会被销毁,而不用static变量存放在栈区方法执行完就会被销毁,每次都重新创建,用static只需要创建一次即可
        static  NSString *iddentfier = @"NB";
        
        //2.当需要一个cell时,tableView对象重用池中根据重用标识去取cell
       UITableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:iddentfier];
        
      //3.根据取出的cell是否为空判断是的创建,还是直接使用
        if (cell == nil) {
            NSLog(@"=====");
            cell = [[[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:iddentfier]autorelease];
        }
    cell展示数据

     return cell;

    }

    在UITableViewController下创建cell --一般使用KVC

    思想:

    1.先创建UITableViewController设置为windows根视图

    2.创建UITablecell ,使用懒加载布局,必须重写cell的自定义方法

    3.创建Model类  ,是存储数据模型的类,在设计的MOder的时候,给这MOder类添加相应的属性,而且MOder的属性名要存储的数据所对应的key值名保持一致(MOder类一般存储的都是字典类型数据).moder类中属性个数,要和需要存储的个数一致
    //好处:moder在实际开发中使用频率非常高,因为moder中的访问时可以直接通过点语法访问,而且他具有的属性会由提示
    //可以使用KVC形式快速为moder对象的属性赋值

    4.赋值可以用属性赋值(重写set方法)和自定义一个接口

    cell的使用

    1.注册GirlCell
        [self.tableView registerClass:[GirlCell class] forCellReuseIdentifier:kGirlcell];

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

       GirlCell *cell = [tableView dequeueReusableCellWithIdentifier:kGirlcell forIndexPath:indexPath];

    }

    cell的自适应

    在UITableViewController下实现

    思路:

    1.在UITablecell.h里用类自定义一个方法

    2.调用一个接口方便外界调用

    使用

    //1.文本绘制的大小,要和summaryLabel的宽一致

    //2.NSStringDrawingUsesLineFragmentOrigin设置文本绘制的标准
    //3.设置文字的属性(注意文字的大小要和summaryLabel上设置的文字大小保持一致)

    //4.文本上下文nil

    eg:

    //调用一个借口方便外界赋值
    - (void)assignNewsCellSubviewByNews:(News *)news;
    //返回cell的高度
    + (CGFloat)cellHeight:(News *)news;

    实现:

    + (CGFloat)sumaryheight:(News *)news{
        CGSize contextSize = CGSizeMake(300, 0);
        NSDictionary *dic = @{NSFontAttributeName :[UIFont boldSystemFontOfSize:15.0]};
    #pragma mark -- 自定以cell
        CGRect rect = [news.summary
    boundingRectWithSize:contextSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        return rect.size.height;
    }

    梦想不是挂在嘴边炫耀的空气,而是需要认真的实践,等到对的风,我们展翅翱翔;没有风,只要拥有足够强壮的翅膀,我们照样拔地飞行。天空见。
  • 相关阅读:
    软件开发模型
    Java学习--数组
    10年软件测试经验
    Log4net
    文件下载界面
    VB.NET中文双引号的处理方法
    SOAPUI使用
    .net数字转换成汉字大写
    SQL调用Webservices
    SQL大小写金额转换
  • 原文地址:https://www.cnblogs.com/size/p/5058962.html
Copyright © 2020-2023  润新知