zoukankan      html  css  js  c++  java
  • UI整理-----part3--UITableView

    1.plist文件

    (1)plist文件是iOS里最简单的一个数据本地化存储方式,我们可以将数据写成数据结构存储到plist文件中永久保存。

    (2)通过 [[NSBundlemainBundle] pathForResource:@"myPlist"ofType:@"plist"] 可以获取plist文件的存储路径

     2.tableView基本知识

    (1)tableView的两种风格:UITableViewStylePlain UITableViewStyleGroup

    (2)UITableView中每行数据都是一个UITableViewCell,在其内部有一个UIView控件(contentView,作为其他元素的父控件),两个UIlabel控件(textLabel、detailTextLabel),一个UIImage控件(imageView),分别用于容器、显示内容、详情和图片。

    (3)UITableViewCell的风格:

        typedefNS_ENUM(NSInteger, UITableViewCellStyle) {

        UITableViewCellStyleDefault,    左侧显示textLabel,不显示detailTextLabel,imageView可选(显示在最左边)

        UITableViewCellStyleValue1,     左侧显示textLabel,右侧显示detailTextLabel,(默认蓝色)imageView可选(显示在最左边)

        UITableViewCellStyleValue2,     左侧依次显示textLabel和detailTextLabel,imageView可选(显示在最左边)

        UITableViewCellStyleSubtitle     左上方显示textLabel,左下方显示detailTextLabel(默认灰色),imageView可选(显示在最左边)

        };

    3.UITableViewDataSource:对于数据源控件,除了代理还有一个数据源属性,通过它和外界进行数据交互。对于UITableView设置完dataSource后实现UITableViewDataSource协议,其中包括——

             <1>返回分组数:- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

             <2>返回每组行数:- (NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section{}

             <3>返回每行的单元格:- (UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath;

             <4>返回每组头标题名称:- (NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section;

             <5>返回每组尾部说明:- (NSString *)tableView:(UITableView *)tableViewtitleForFooterInSection:(NSInteger)section;

             <6>返回每组标题索引:- (nullableNSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView;

          生成单元格的方法并不是一次全部调用,而是只会生产当前显示在界面的单元格行数+1,当用户滚动操作时再显示其它单元格。

    4.UITableViewDelegate:代理实现的方法:

             <1>设置行高:

                    设置分组标题内容高度:- (CGFloat)tableView:(UITableView *)tableViewheightForHeaderInSection:(NSInteger)section

                    设置每个cell的高度:- (CGFloat)tableView:(UITableView *)tableViewheightForRowAtIndexPath:(nonnullNSIndexPath *)indexPath

             <2>监听点击:- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath

             <3>单元格的复用:通过在创建单元格之前首先以标识符为参考到复用池中查找是否存在cell对象,有就复用,没有则创建: - (nullable__kindofUITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;

    5.UITableView右侧图标 ——访问器

             <1>设置访问器:@property (nonatomic) UITableViewCellAccessoryType    accessoryType;

        typedefNS_ENUM(NSInteger, UITableViewCellAccessoryType) {

        UITableViewCellAccessoryNone,                                                         不显示任何图标  

        UITableViewCellAccessoryDisclosureIndicator,                                     跳转指示图标       

        UITableViewCellAccessoryDetailDisclosureButton,                                内容详情图标和跳转指示图标

        UITableViewCellAccessoryCheckmark,                                                勾选图标

        UITableViewCellAccessoryDetailButton                                                内容详情图标

        }; 

             <2> UITableViewCell创建的cell可以通过 [cell.contentView addSubview:<#(nonnullUIView *)#>] 方法在cell上添加view

    6.MVC模式:在iOS中多数数据源实现的是视图(view)控件上都有一个dataSource属性用于和控制器(controller)交互,而数据来源我们一般会以数据类型(Model)的形式进行定义。即   model→ controller↔view

    7.编辑状态

    (1)如果定义UITableViewCell,在tableViewCell的contentView的最左边放UIImageView,当cell选中的时候,cell的contentView向右移动,让UIImageView显示出来,只要实现自定义的cell中 - (void)setEditing:(BOOL)editinganimated:(BOOL)animated; 可将其置于编辑模式

    (2)通过按钮实现可编辑与非编辑状态转换:

    - (IBAction)buttonAction:(UIButton *)sender {

        sender.selected = !sender.selected;

        [_tableView setEditing:!_tableView.editing animated:YES];

        if (_tableView.editing == NO) {

            [_tableView reloadData];

        }

    }

    (3)数据源方法中通过以下方法,改变单元格是否可编辑:

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

    {

        if (indexPath.row == 0) {

            return NO;    //不可编辑

        }

        return YES;

    }

     (4)UITableView的delegate里有设置编辑模式下按钮的样式:

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

    {

        if (indexPath.row == 1) {

            return UITableViewCellEditingStyleInsert;

        }

        return UITableViewCellEditingStyleDelete;

        /*

         共有三种模式,即:

         typedef NS_ENUM(NSInteger, UITableViewCellEditingStyle) {

         UITableViewCellEditingStyleNone,     无编辑

         UITableViewCellEditingStyleDelete,   删除编辑

         UITableViewCellEditingStyleInsert     插入编辑

         };

         */

    }

    (5)按照点击实现新增和删减的方法(dataSource中的方法)

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

        if (editingStyle == UITableViewCellEditingStyleInsert) {

         

            [_dataList insertObject:@""atIndex:indexPath.row + 1];    //NSMutableArray *_dataList;

            NSIndexPath *indexP = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];   //构造插入位置,引用索引

            NSArray *array = @[indexP];

            [_tableView insertRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationLeft];

        }elseif (editingStyle == UITableViewCellEditingStyleDelete){

            [_dataList removeObjectAtIndex:indexPath.row];

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

            //如果上面两个隐藏其一,都会崩

        }

    }

     

    8.单元格自适应

    -(CGFloat)tableView:(UITableView *)tableViewheightForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        NSString *text = [self.news DataobjectAtIndex:indexPath.row];

        CGSizesize = [text sizeWithFont:[UIFontsystemFontOfSize:20] constrainedToSize:CGSizeMake(350, 999999) lineBreakMode:NSLineBreakByWordWrapping];

        returnsize.height + 100;

        NSLineBreakByTruncatingHead         以单词为单位换行,以字符为单位截断

        NSLineBreakByClipping                  以字符为单位换行,以字符为单位截断

        NSLineBreakByCharWrapping        以单词为单位换行,以字符为单位截断

    }

    9.UITableViewCell的相关

    (1)通过UITableViewCell的contentView添加子视图,在UITableViewCell上有一个透明的子视图叫contentView,可以通过向contentView上 加载视图来定制单元格,Cell的contentView比Cell少一个像素,UITableViewCell 有个 Separator 高度为1。如果在TableView中设置Separator为None,那么Cell的contentView高度就会和 Cell高度一致了。

    10.UIRefreshControl:iOS6之后,可以为tableView添加一个UIRefreshControl,系统自动给其在所在进行放置。

        //1.创建下拉刷新控件

        UIRefreshControl *refreshC = [[UIRefreshControlalloc] init];

        //2.增加title

        refreshC.attributedTitle = [[NSAttributedStringalloc] initWithString:@"下拉刷新"];

        //3.增加一个点击事件

        [refreshCaddTarget:selfaction:@selector(buttonAction:) forControlEvents:UIControlEventValueChanged];

        //控件的父类是UIView

        UIButton *button = [UIButtonbuttonWithType:UIButtonTypeContactAdd];

        button.frame = CGRectMake(100, 10, 30, 30);

        [buttonaddTarget:selfaction:@selector(a:) forControlEvents:UIControlEventTouchUpInside];

        [refreshCaddSubview:button];

        [tableViewaddSubview:refreshC];

      

         

  • 相关阅读:
    [转][黄忠成]Object Builder Application Block (1)
    C#.NET里面抽象类和接口有什么区别
    MVC中AOP思想的体现(四种过滤器)并结合项目案例说明过滤器的实际用法
    NHibernate之(24):探索NHibernate二级缓存(下)
    使用 ES (elasticsearch) 搜索中文
    elasticsearch 中文 term & completion suggester
    uwsgs loading shared libraries: libicui18n.so.58 异常处理
    tensorflow with gpu 环境配置
    Java 多线程执行
    SpringBoot log4j2 异常
  • 原文地址:https://www.cnblogs.com/8023huihui/p/5200547.html
Copyright © 2011-2022 走看看