zoukankan      html  css  js  c++  java
  • UITableView随笔笔记

    UITableView 继承自UIScrollView,所以可以滚动,但只能是纵方向上的。

    UITableView由section和cell组成,填充的内容来自数据源,一般数据源由ViewController作为代理,因此需要遵循它的两个协议,分别是UITableViewDataSource 和 UITableViewDelegate。

    UITableView的简单实现步骤:

    1. 设置UITableView在视图中,可以表现为Plain和Grouped两种风格;

    2. 将UITableView的dataSource设置为ViewController,并让ViewController遵循UITableViewDataSource协议;

    3. 利用DataSource协议设置UITableView的sections有多少个,调用方法numberOfSectionsInTableView,返回sections的个数;

    4. 设置UITableView的每个section中有多少行,调用方法numberOfRowsInSection,返回sections的行数;

    5. 设置UITableView中sections的首部标题,调用方法titleForHeaderInSection,尾部标题方法为titleForFooterInSection;

    6.1 设置cell中显示的内容,调用方法cellForRowAtIndexPath。通过传入的(NSIndexPath *)indexPath来确定具体的row来给定内容,最终返回UITableViewCell类型的cell对象;

    6.2 其中NSIndexPath包含了section和row,可以准确定位是哪个section中的哪个row;

    6.3 取得的数据内容将传递给UITableViewCell中的textLable的text属性

    UITableViewCellStyle有四种模式,当设置为UITableViewCellStyleSubtitle的时候可以增加副标题文字说明效果;

    依然在cellForRowAtIndexPath中设置UITableView的相关属性:

    6.4 在UITableViewCell中有imageView属性,可以设置每个cell的图标,将图片名传给image属性;

    6.5 副标题设置是detailTextLabel,是个UILabel,可设置内容颜色等;

    6.6 accessoryType可设置accessory类型,是选项或是箭头指示;

     

    如果修改了数据模型,需要让viewcontroller通知view刷新一下,刷新方法分为全部和局部;

    1.reloadData 将刷新全部数据

    2.reloadRowsAtIndexPaths 刷新局部数据

    - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

    indexPaths是NSIndexPath类型的数组,animation为枚举类型的UITableViewRowAnimation,设置刷新的动画效果;

    UITableView的数据优化

    之前6.1中设置cell中显示的内容,调用方法cellForRowAtIndexPath内,在对UITableViewCell实例化的时候,用到的:

    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

    其中reuseIdentifier就是设置重用标示符,将不显示的数据会丢入缓存池中,然后即将显示的cell会在缓存池中通过标示符来查找,如果找到该标示符的cell就直接拿过来重用,避免重新实例化消耗内存影响性格,达到优化目的;如果没找到对应标示符的cell就实例化一个。具体代码如下:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *reuseIdentifierStr = @"myCell";
        //在缓冲池中寻找
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifierStr];
        //如果没找到,就实例化一个新的cell
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifierStr];
        }
        Product *p = data[indexPath.row];
        cell.textLabel.text = p.name;
        cell.imageView.image = [UIImage imageNamed:p.icon];
        cell.detailTextLabel.text = p.detail;
        cell.detailTextLabel.textColor = [UIColor grayColor];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        return cell;
    }

    7. UITableView中cell的添加删除操作,使用到Toolbar的添加删除按钮

    7.1 在添加删除按钮中设置UITableViewCellEditingStyle的标识,1为删除,2为添加。因为该枚举类型是只读的,所以只能通过对tableview的tag进行设置,来识别是添加操作还是删除操作。

    - (IBAction)action:(UIBarButtonItem *)sender {
        if (sender.tag == 1) {
            _tableView.tag = UITableViewCellEditingStyleDelete;
        }
        else{
            _tableView.tag = UITableViewCellEditingStyleInsert;
        }
        BOOL isEdit = _tableView.isEditing;
        [_tableView setEditing:!isEdit];
    }

    7.2 标识需要在方法editingStyleForRowAtIndexPath中的返回值设置,这样就知道了是删除操作还是添加操作。

    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
        return _tableView.tag;
    }

    7.3 相应的操作需要在commitEditingStyle中实现

    - (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
        //删除操作
        if (editingStyle == 1) {
            //删除数据
            [_dataList removeObjectAtIndex:indexPath.row];
            //更新cell
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
        }else{
            //添加数据
            [_dataList insertObject:@"新建数据" atIndex:indexPath.row + 1];
            //更新cell
            NSIndexPath *insertIndexPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
            [tableView insertRowsAtIndexPaths:@[insertIndexPath] withRowAnimation:UITableViewRowAnimationTop];
        }
    }
  • 相关阅读:
    Foundation与Core Foundation内存管理基本原则简述
    Quartz2D 图像处理
    XCode中的特殊快捷键图标
    UITableView的编辑模式
    Cocoa框架中的内存分区
    Cocoa 框架为什么采用两阶段的方式来创建对象?
    IOS中的内存不足警告处理(译)
    Caching和Purgeable Memory (译)
    3年后,我回来了
    POJ3632 Optimal Parking
  • 原文地址:https://www.cnblogs.com/litaowei/p/3710293.html
Copyright © 2011-2022 走看看