iPhone开发 UITableView代码实现实例

移动开发 iOS
本文介绍的iPhone开发 UITableView代码实现实例,主要介绍了UITableView的使用方法,我们先来看内容。

iPhone开发 UITableView代码实现实例是本文要介绍的内容,介绍了UITableView的基本操作,以小实例实现,我们来看内容。
 
1、前题条件

1.1 UITableView不能直接加载到窗体中,窗体中首先要加载一个UIView控件,然后才可以添加UITableView

1.2 UITableView需要继续两个协议<UITableViewDelegate,UITableViewDataSource>

两个协议的方法为:

--行的数量:

  1. -(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section   
  2. {  
  3.  return [tableArray count];  
  4. }  
  5.  
  6. --行的定义  
  7.  
  8. -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  9. {  
  10.  static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";  
  11.    
  12.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:  
  13.                              SimpleTableIdentifier];  
  14.     if (cell == nil)  
  15.  {  
  16.   //默认样式  
  17.   cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  
  18.                                    reuseIdentifier: SimpleTableIdentifier] autorelease];  
  19.  }  
  20.  //文字的设置  
  21.  NSUInteger row=[indexPath row];  
  22.  cell.textLabel.text=[tableArray objectAtIndex:row];  
  23.  return cell;  

2、添加UIView

  1.  UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320,260)];  
  2. //自定识别头及高度  
  3.  view.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight;  
  4.  view.backgroundColor = [UIColor blueColor];  
  5. //将view添加到主窗体  
  6. [self.view addSubview:view]; 

3、添加UITableView

  1. UITableView  *table1=[[UITableView alloc] initWithFrame:CGRectMake(0, 0.00, 300, 250)];  
  2.  table1.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight;  
  3. table1.backgroundColor=[UIColor redColor];  
  4. //添加到view窗体上  
  5. [view addSubview:table1];  
  6.  table1.delegate=self;  
  7.  table1.dataSource=self

4、取消点击动画

  1. [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

5、滚动路径标识,直至指数连续接收器在屏幕上的特定位置

  1. - (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:
  2. (BOOL)animated Parameters   

indexPath

索引路径标识在其行的索引,其部分指标表视图行。

scrollPosition

 一个常数,在接收标识表查看行(上,中,下)的相对位置滚动时结束。

animated

是如果你要动画在位置改变,没有是否应立即生效。

实例代码:

  1. float height = 216.0;  
  2. CGRect frame = m_pTableView.frame;  
  3. frame.size = CGSizeMake(frame.size.width, frame.size.height - height);  
  4. [UIView beginAnimations:@"Curl"context:nil];//动画开始     
  5. [UIView setAnimationDuration:0.30];     
  6. [UIView setAnimationDelegate:self];    
  7. [m_pTableView setFrame:frame];    
  8. [m_pTableView scrollToRowAtIndexPath:selectedIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];  
  9. [UIView commitAnimations];  

6、获取选中的列,如图所示:

iPhone开发 UITableView代码实现实例

代码如下:

  1. for (NSInteger i = 0; i < [[peopleTableView visibleCells] count]; i++)   
  2. {  
  3. UITableViewCell *cell = [[peopleTableView visibleCells] objectAtIndex:i];  
  4. if (cell.accessoryType == UITableViewCellAccessoryCheckmark) //将勾选的人员添加到列表中  
  5. {  
  6. Item *item = (Item *)[NextPeopleList.SelectList objectAtIndex:i];  
  7. [peopleList addObject:item];  
  8. isCheck = TRUE;  
  9. }  

7、Cell将要显示时事件

  1. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3. if (indexPath.row % 2 == 0) {  
  4. cell.backgroundColor = DarkColor;  
  5. }else {  
  6. cell.backgroundColor = LightColor;  

小结:iPhone开发 UITableView代码实现实例的内容介绍完了,希望本文对你有所帮助!

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

2011-07-28 10:11:54

iPhone开发 备忘

2011-08-08 10:42:46

iPhone UITableVie 分页

2011-07-27 11:14:37

iPhone UITableVie

2011-08-15 13:44:07

iPhone开发UITableView

2011-07-25 18:02:51

iPhone LibFetion 移植

2011-08-19 10:01:09

iPhone应用SqliteUITableView

2011-08-08 16:56:44

iPhone 字符处理 视图

2011-07-25 14:44:41

iPhone iPhone开发 截屏

2011-08-02 17:14:41

iPhone应用 UITableVie

2011-07-22 17:24:46

iPhone 视图

2011-08-15 11:23:41

iPhone开发循环滚动UIScrollVie

2011-08-01 13:13:19

iPhone开发 图片

2009-07-22 11:27:36

iBATIS模糊查询

2009-09-01 16:59:25

C#画直线

2009-08-27 18:09:49

C#接口的实现

2009-09-01 13:59:01

C#操作Excel

2009-09-09 12:55:59

C# TextBox事

2009-08-17 14:41:47

C#进度条实现

2011-08-22 14:31:53

iPhone开发

2011-08-22 13:46:15

iPhone开发GameKit 蓝牙
点赞
收藏

51CTO技术栈公众号