iPhone开发应用中关于UITableView详细教程

移动开发 iOS
本文主要是来学习UITableView的详细操作。UITableView是一个很强大的控件,在我们iphone开发过程中会经常用到。

iPhone开发应用中关于UITableView详细教程是本文要介绍的内容,主要是来学习UITableView的详细操作。UITableView是一个很强大的控件,在我们iphone开发过程中会经常用到。

下面我做以下简单介绍

‍UITableView有一个基本元素的索引NSIndexPath,你可以通过索引NSIndexPath找到‍UITableView下面的子元素只要这个方法

  1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  2.    //在这个方法里你可以根据NSIndexPath判断相应的元素,然后做处理 

例如:

  1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3. if(indexPath.section == 5 && indexPath.row ==2)return;  
  4. }  
  5. UITableView的每一个子元素(Entity)称为UITableViewCell,UITableViewCell由下面这个方法初始化  
  6. -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath  
  7. //这个方法是必需的,他是产生UITableView内容的必须载体 

例如:

  1. -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath  
  2. {//因为UITableView由很好的内存控制机制,他每次只加载一屏幕的cell(7个左右),当用户触摸移动时,动态加载新产生的cell  
  3. static NSString *RootViewControllerCell =@"HelpViewControllerCell";//产生一个静态标识  
  4. UITableViewCell* cell =(UITableViewCell*)[tableViewdequeueReusableCellWithIdentifier:RootViewControllerCell];//标记新产生的cell  
  5. if(cell==nil) //如果cell不为空标识cell还没有被释放,还在屏幕中显示  
  6. {  
  7. cell =[[[UITableViewCellalloc]initWithStyle:UITableViewCellSeparatorStyleSingleLinereuseIdentifier:RootViewControllerCell]autorelease];  
  8. }  
  9. return cell;  
  10. } 

UITableViewCell通过NSIndexPath索引,正如上面所看到的,NSIndexPath索引由两个属性section和row

section通常就是我们所谓的段,row所谓的段内的行

我们可以通过下面这个方法返回你想要在UITableView中显示段数

  1. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; //通常情况下我们看到的都是一段,左移这个方法不是必须的 

我们可以通过下面这个方法返回你想要在UITableView中的某一段中显示的行数

  1. - (NSInteger)tableView:(UITableView *)tView numberOfRowsInSection:(NSInteger)section;//通常情况下是一段,所以不必判断段数 

假如您有很多段,而且每一个段的显示行数还不一样,你就要通过上面的方法精确控制,例如:

  1. - (NSInteger)tableView:(UITableView *)tView numberOfRowsInSection:(NSInteger)section {  
  2.       if(section == 0) return 1;  
  3.       if(section == 1) return 1;  
  4.    if(section == 2) return 8;  
  5.    if(section == 3) return 1;  
  6.    if(section == 4 || section ==5)   return 3;  
  7.    if(section == 6)return 4;  
  8.    return0;  

这个方法时必须的。

另外我们可以通过下面这个方法精确控制某一行的高度

  1. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {  
  2.  
  3.    if(indexPath.section == 0 && indexPath.row ==0)    return80;  
  4.    return 40.0;  

另外我们可以通过下面这个方法精确控制某一段的高度

  1. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section  
  2. {  
  3.    if (section == 0)return60.0;  
  4.    return40.0;  

另外我们还可以通过下面这个方法精确控制某一段的标题和视图

  1. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;  
  2.  
  3. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; 

这两个方法鄙视必须的,我就不作介绍。

拥有以上的及格方法你就可以完成超炫的UITableView视图了。前提是要活灵活用,还有其他几个常用的控件,这里就不在写了,需要的可以留言交流。

小结:iPhone开发应用中关于UITableView详细教程的内容介绍完了,希望本文对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-08-02 17:14:41

iPhone应用 UITableVie

2011-07-27 11:14:37

iPhone UITableVie

2011-08-09 17:12:30

iPhoneCFRunLoop

2011-07-06 17:40:43

iPhone SDK

2011-07-08 14:58:16

iPhone Xcode iOS

2011-08-08 14:07:49

iPhone开发 字体

2011-08-18 10:39:46

iPhone开发界面

2011-08-12 10:09:23

iPhone开发多线程

2011-07-27 11:19:33

iPhone UITableVie

2011-08-09 13:10:32

iPhone地图开发

2011-08-19 10:35:19

iPhone应用Three20

2011-08-18 10:06:10

2011-08-09 14:42:07

iPhonePCM播放器

2011-08-08 18:19:09

iPhone音频播放

2011-08-10 10:23:20

iPhoneArchivingNSCoder

2011-07-29 13:27:48

iPhone 开发 Nib

2011-08-10 17:37:00

iPhoneASIHTTPRequ

2011-08-19 10:01:09

iPhone应用SqliteUITableView

2011-08-18 15:24:40

iPhone国际化

2011-08-22 15:15:49

iPhone开发NSMutableAr排序
点赞
收藏

51CTO技术栈公众号