zoukankan      html  css  js  c++  java
  • UITableView

    在iPhone和其他iOS的很多程序中都会看到Table View的出现,除了一般的表格资料展示之外,设置的属性资料往往也用到Table View,Table View主要分为以下两种:

    • Plain:普通的列表模式
    •  Grouped :分组模式

    对于UITableView,我們有一些特殊的概念和术语,比如说我们成Table View的一行为Cell,而许多的Cell可以组成Section,每个Section上下又分別有Header和Footer,许多个的Section则组成了整个Table ,当然Table也有Header和Footer。

    1、Table View放上控件
    打开ViewController.xib文件,往ViewController.xib界面上拖拽一个Table View控件到现有的View上,对齐
    2、连接新添加的TableView和ViewController。
    选中新添的TableView控件,打开连接检查器(Connection Inspector), 找到delegate和datasource并点中圆圈拉线连接到左边File's Owner图标上,为什么要把这两个连接File's Owner上呢?这是因为iOS使用的MVC设计模式,View和ViewController之间的对应关系,需要一个桥梁来进行连接的(即,对于一个视图,他如何知道自己的界面的操作应该由谁来响应),这个桥梁就是File's Owner
    3、打开ViewController.h,添加协议和Property (类似与java里的实现接口)
    [cpp] view plain copy
    1. #import <UIKit/UIKit.h>  
    2.   
    3. @interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>  
    4. @property (strong, nonatomic) NSArray *list;  
    5. @end  

    4、打开.m文件,添加:
    [cpp] view plain copy
    1. @synthesize list = _list;  

    这是发现有两个警告,提示未完成的实现,这提示的是UITableViewDelegate, UITableViewDataSource这个两个头文件里的协议的方法未实现。待会我们去实现它。
    5、建立数据
    [cpp] view plain copy
    1. - (void)viewDidLoad  
    2. {  
    3.     [super viewDidLoad];  
    4.     // Do any additional setup after loading the view, typically from a nib.  
    5.     NSArray *array = [[NSArray alloc] initWithObjects:@"美国", @"菲律宾",  
    6.                       @"黄岩岛", @"中国", @"泰国", @"越南", @"老挝",  
    7.                       @"日本" , nil];   
    8.     self.list = array;   
    9. }  
    10.   
    11. - (void)viewDidUnload  
    12. {  
    13.     [super viewDidUnload];  
    14.     // Release any retained subviews of the main view.  
    15.     self.list = nil;  
    16.       
    17. }  
    6、生成row
    关键的步骤来了,实现tableview添加数据源,返回TableView的行数,返回各行cell实例。
    [cpp] view plain copy
    1. - (UITableViewCell *)tableView:(UITableView *)tableView   
    2.          cellForRowAtIndexPath:(NSIndexPath *)indexPath {   
    3.       
    4.     static NSString *TableSampleIdentifier = @"TableSampleIdentifier";   
    5.       
    6.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:   
    7.                              TableSampleIdentifier];   
    8.     if (cell == nil) {   
    9.         cell = [[UITableViewCell alloc]   
    10.                 initWithStyle:UITableViewCellStyleDefault   
    11.                 reuseIdentifier:TableSampleIdentifier];   
    12.     }   
    13.       
    14.     NSUInteger row = [indexPath row];   
    15.     cell.textLabel.text = [self.list objectAtIndex:row];   
    16.     return cell;   
    17. }  
    上面的第二个方法中,
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: TableSampleIdentifier];
    这个语句根据标识符TableSampleIdentifier寻找当前可以重用的UITableViewCell。当某行滑出当前可见区域后,我们重用它所对应的UITableViewCell对象,那么就可以节省内存和资源。
    这里UITableViewCellStyleDefault是表示UITableViewCell风格的常数,除此之外,还有其他风格,后面将会用到。
    注意参数(NSIndexPath *)indexPath,它将行号row和部分号section合并了,通过[indexPath row];获取行号。之后给cell设置其文本:
    cell.textLabel.text = [self.list objectAtIndex: row];

  • 相关阅读:
    Django url处理
    Django从请求到返回流程
    Django快速开发投票系统
    Python3集成安装xadmin
    解读TCP 四种定时器
    前后端交互中出现的问题(二)
    前后端交互中出现的问题(一)
    IDEA总是启动不了
    安装maven时遇到的问题
    Java基础笔记(十九)——抽象类abstract
  • 原文地址:https://www.cnblogs.com/dingfuyan/p/5209972.html
Copyright © 2011-2022 走看看