你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> 如何在tableView的數據源和代理事件之外獲取當前點擊的是哪一行

如何在tableView的數據源和代理事件之外獲取當前點擊的是哪一行

編輯:IOS開發綜合
[cpp] view plaincopy
<pre name="code" class="cpp"> NSIndexPath *indexPathOfOwnerCell = nil; 
  /* Also, make sure the selected text field is visible on the screen */ 
//獲取每個section裡共有多少個cell 
  NSInteger numberOfCells = [self.myTableView.dataSource  
                             tableView:self.myTableView 
                             numberOfRowsInSection:0]; 
    NSLog(@"numberOfCells=%d",numberOfCells); 
  /* So let's go through all the cells and find their accessory text fields.
   Once we have the refernece to those text fields, we can see which one of
   them is the first responder (has the keyboard) and we will make a call
   to the table view to make sure after the keyboard is displayed,
   that specific cell is NOT obstructed by the keyboard */ 
  for (NSInteger counter = 0; 
       counter < numberOfCells; 
       counter++){ 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:counter 
                                                inSection:0]; 
    UITableViewCell *cell = [self.myTableView cellForRowAtIndexPath:indexPath]; 
    UITextField *textField = (UITextField *)cell.accessoryView; 
    if ([textField isKindOfClass:[UITextField class]] == NO){ 
      continue; 
    } 
    if ([textField isFirstResponder]){ 
      indexPathOfOwnerCell = indexPath; 
        NSLog(@"indexPath.section=%d",indexPath.section); 
        NSLog(@"indexPath.row=%d",indexPath.row); 
      break; 
    } 
  } 
   
  [UIView commitAnimations]; 
   
  if (indexPathOfOwnerCell != nil){ 
    [self.myTableView scrollToRowAtIndexPath:indexPathOfOwnerCell 
                            atScrollPosition:UITableViewScrollPositionMiddle 
                                    animated:YES]; 
  } 
</pre><br> 
<pre></pre> 
<br> 
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved