zoukankan      html  css  js  c++  java
  • 【ios开发】控件细究1:UITableView

          工作了将近两个月,共接手两个项目,在项目中用的最多的就是UITableView了,但是也是问题出现的最多的地方,由于一开始不熟练,导致很多问题花了很长时间才解决。所以利用这两天空闲时间,好好梳理一下这个控件,希望能够方便以后的开发工作。

         我的介绍是从我创建UITableView的顺序说起。

    一、概述:

         UITableView继承与UIScrollView。这两个控件的异同点如下:

          相同点:UITableView实现了UIScrollView协议,且继承于UIScrollView,两个组件都可以滑动。 

        相异点:UITableView是逐步加载UITableViewCell,以后每次滚动时,重绘cell;UIScrollView是初始化所有其子视图,以后每次滚动,不再重绘其子视图,所以UITableView滑动起来比较卡,而UIScrollView滑动比较顺畅;在数据量比较多的情况下,应该使用UITableView,因为cell可以复用,使用内存相对较少,而UIScrollView使用内存较多;另外UITableView可以刷新数据,reloaddata等,而scrollview的数据是固定的,除非重新初始化。

    二、创建UITableView

        我正常创建UITableView,会把它封装到一个方法里面。这样清楚一点。 

        

    -(void)initTableView
    {
        helpTableView = [[UITableView alloc]initWithFrame:CGRectMake(5, navheight + 7.5, 305, 480) style:UITableViewStyleGrouped];
        helpTableView.delegate = self;
        helpTableView.dataSource = self;
      
        helpTableView.backgroundView = nil;
      [helpTableView setBackgroundColor:[UIColor colorWithRed:231.0/255.0 green:231.0/255.0 blue:231.0/255.0 alpha:1.0]];
    
        helpTableView.separatorColor = [UIColor clearColor];
        helpTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
        helpTableView.scrollEnabled = NO;
        [self.view addSubview:helpTableView];
    [helpTableView release];
    }

    1、首先介绍第一句代码,UITableView的style。

    UITableViewStyleGrouped和UITableViewStylePlain的区别我认为就是UITableViewStyleGrouped背后贴了一层背景图。但是我们可以通过

     helpTableView.backgroundView = nil;
      [helpTableView setBackgroundColor:[UIColor colorWithRed:231.0/255.0 green:231.0/255.0 blue:231.0/255.0 alpha:1.0]];

    这段代码来去除背景。然后绘制上你需要的背景色。helpTableView.backgroundView = nil;这句代码一定要有。

    2、dataSource和delegate

          在初始化UITableView的时候必须实现UITableView的是,在.h文件中要继承UITableViewDelegate和UITableViewDataSource,并实现3个UITableView数据源方法和设置它的delegate为self,这个是在不直接继承UITableViewController实现的方法。

          dataSource是UITableViewDataSource类型,主要为UITableView提供显示用的数据(UITableViewCell),指定UITableViewCell支持的编辑操作类型(insert,delete和 reordering),并根据用户的操作进行相应的数据更新操作,如果数据没有根据操作进行正确的更新,可能会导致显示异常,甚至crush。

           delegate是UITableViewDelegate类型,主要提供一些可选的方法,用来控制tableView的选择、指定section的头和尾的显示以及协助完成cell的删除和排序等功能。

           dataSource和delegate他们其实是Cocoa框架的一种设计模式,叫策略模式,改天可以另外开一篇博文介绍专门介绍策略模式。

    3、UITableView的一些属性

        关于属性和一些方法可以参考这篇文章,讲的比较详细。

    三、实现代理必须实现的三个方法。

     1、UITableView有三个必须实现的核心方法,分别如下:

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

    这个方法可以分段显示或者单个列表显示我们的数据。如下,左边为分段显示,右边为单个列表显示:

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

    这个方法返回每个分段的行数,不同分段返回不同的行数可以用switch来做,如果是单个列表就直接返回单个你想要的函数即可。

    -(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath;

    这个方法是返回我们调用的每一个单元格。通过我们索引的路径的section和row来确定。

     2、UITableView的委托方法

    使用委托是为了响应用户的交互动作,比如下拉更新数据和选择某一行单元格,在UITableView中有很大这种方法供我们选择。

    //设置Section的数量  
    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{  
     return TitleData;  
    }  
    //设置每个section显示的Title  
    - (NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section{  
     return @"Andy-清风";  
    }  
      
    //指定有多少个分区(Section),默认为1  
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  
     return 2;  
    }  
      
    //指定每个分区中有多少行,默认为1  
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
    }  
      
    //设置每行调用的cell  
    -(UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath {  
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";  
        
        UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:  
                                 SimpleTableIdentifier];  
        if (cell == nil) {    
            cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefault  
                                           reuseIdentifier:SimpleTableIdentifier] autorelease];  
     }  
     cell.imageView.image=image;//未选cell时的图片  
     cell.imageView.highlightedImage=highlightImage;//选中cell后的图片  
     cell.text=@”Andy-清风”;  
     return cell;  
    }  
    //设置让UITableView行缩进  
    -(NSInteger)tableView:(UITableView *)tableViewindentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{  
     NSUInteger row = [indexPath row];  
     return row;  
    }  
    //设置cell每行间隔的高度  
    - (CGFloat)tableView:(UITableView *)tableViewheightForRowAtIndexPath:(NSIndexPath *)indexPath{  
        return 40;  
    }  
    //返回当前所选cell  
    NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:section];  
    [TopicsTable selectRowAtIndexPath:ip animated:YESscrollPosition:UITableViewScrollPositionNone];  
      
    //设置UITableView的style  
    [tableView setSeparatorStyle:UITableViewCellSelectionStyleNone];  
    //设置选中Cell的响应事件  
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{  
      
     [tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失  
    }  
      
    //设置选中的行所执行的动作  
      
    -(NSIndexPath *)tableView:(UITableView *)tableViewwillSelectRowAtIndexPath:(NSIndexPath *)indexPath  
    {  
        NSUInteger row = [indexPath row];  
         return indexPath;  
    }  
    //设置划动cell是否出现del按钮,可供删除数据里进行处理  
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath {  
    }  
    //设置删除时编辑状态  
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle  
    forRowAtIndexPath:(NSIndexPath *)indexPath  
    {  
    }  
    //右侧添加一个索引表  
    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{  
    }  

    参考链接:

    http://www.zhouwenyi.com/node/6212

    http://www.zhouwenyi.com/node/6218

  • 相关阅读:
    sql递归
    Sql Server随机抽取数据效率优化
    sql 左位补齐
    sql语句读取xml
    sql存储过程返回值
    sql 高效随机获取大表数据
    删除临时表
    sql完整事务
    加载静态文件,父模板的继承和扩展
    开始Flask项目
  • 原文地址:https://www.cnblogs.com/ymonke/p/3461285.html
Copyright © 2011-2022 走看看