iPhone開發 UITableView代碼實現執行個體

來源:互聯網
上載者:User

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、擷取選中的列,:

代碼如下:

 
  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代碼實現執行個體的內容介紹完了,希望本文對你有所協助!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.