2011-05-24 29 views
29

如何設置默認選擇的行?我想選擇一行,將所選行的編號與indexpath.row對齊,然後重新加載表並選擇所選行。有人能幫助我嗎?我可以在方法中獲取選定的行嗎?(IBActionsegmentedControlIndexChanged?不在didSelectRowAtIndexPath如何在UITableView中默認重新加載後記住選定的行並對其進行維護?

這是我的代碼,如果它可以幫助你瞭解

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    //lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(200, 10, 100, 20) ]; 

    // Configure the cell... 
    NSString *depdateChoosed=[deparatureDates objectAtIndex:depSelectedIndice]; 
    NSString *comeateChoosed=[deparatureDates objectAtIndex:comSelectedIndice]; 
    NSString *choosedDate =[NSString stringWithFormat:@"%@%@",depdateChoosed, comeateChoosed]; 
    NSLog(@"date choisi %@ ",choosedDate); 

    if(segment.selectedSegmentIndex==0) 
    { 
     cell.textLabel.text =[deparatureDates objectAtIndex:indexPath.row];   
    } 
    else if(segment.selectedSegmentIndex==1) { 
     //cell.textLabel.text =[goingBackDates objectAtIndex:indexPath.row]; 
     //cell.textLabel.text=[goingBackDates objectAtIndex:indexPath.row]; 
     cell.textLabel.text =[goingBackDates objectAtIndex:indexPath.row]; 
    } 
    return cell; 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@" champ séléctionner : %d ",indexPath.row); 

    if(segment.selectedSegmentIndex==0) 
    { 
     depSelectedIndice=indexPath.row; 
    } 
    else if(segment.selectedSegmentIndex==1) { 
     comSelectedIndice=indexPath.row; 
    } 
    //[tableView reloadData]; 
} 

-(IBAction) segmentedControlIndexChanged 
{ 
    int i; 
    switch (self.segment.selectedSegmentIndex) 
    { 
     case 0: 
      i=0; 

      [tableView reloadData]; 
      break; 
     case 1: 
      i=1; 
      NSLog(@"dexieme segment selectionner"); 
      [tableView reloadData]; 
     default: 
      break; 
    } 
} 

回答

64
NSIndexPath *indexPath = [tableView indexPathForSelectedRow]; 

會給你NSIndexPath當前選定行。你可以做

[tableView selectRowAtIndexPath:indexPath 
         animated:NO 
       scrollPosition:UITableViewScrollPositionMiddle]; 

稍後選擇該行(並將其顯示在屏幕中間)。

+0

方法NSIndexPath indexPath = [tableView indexPathForSelectedRow];不能返回int? – user567 2011-05-25 00:09:38

+0

錯字,對不起。它是一個指向索引路徑的指針。 – 2011-05-25 00:10:30

+26

另外,確保你的'UITableViewController'上的'clearsSelectionOnViewWillAppear'設置爲'NO',否則當視圖出現時它會自動取消選擇該行。這讓我困擾了好幾個小時。 – cheeesus 2013-04-06 12:08:04

5

這是你如何在一個UITableView預選中的一行:

[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition: UITableViewScrollPositionNone]; 

設置indexPath是你要選擇的行,然後進行方法調用如上。

+0

謝謝,但我不能使用int? tableView selectRowAtIndexPath:3? – user567 2011-05-25 00:12:05

+0

你不能使用整數。你必須傳入一個indexPath,如這裏的許多答案所示。希望它能幫助別人。 – Fattie 2014-05-13 15:53:36

9

這將做到這一點:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section]; 
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition: UITableViewScrollPositionNone]; 

是你要選擇的細胞數(0開始...),以及部分是在細胞出現的部分。

+0

我有一個'UITableView' +130行,這對我有用。我在'viewWillAppear'中調用它。 +1謝謝 – youssman 2015-04-22 02:22:17

30

我有同樣的問題,這是我做的:

在你tableViewController,在這個函數

(UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    if(indexPath.row == 'the row you want to select') 
    { 
     [tableView 
      selectRowAtIndexPath:indexPath 
      animated:TRUE 
      scrollPosition:UITableViewScrollPositionNone 
     ]; 

     [[tableView delegate] 
      tableView:tableView 
      didSelectRowAtIndexPath:indexPath 
     ]; 

    } 

} 
+1

謝謝...它爲我工作 – Ronak 2013-06-11 20:10:48

+0

是的,這樣選擇一個單元格,當它被處理到tableView時,確保即使你調用[reloadData]它也會被選中。這似乎比在[viewWillAppear]中實現它更好。 謝謝! – pimguilherme 2014-03-25 17:54:27

+0

如果您的表格視圖顯示動畫,這是唯一的解決方案。不要在viewDidLoad/viewDidAppear中選擇默認單元格! – iOSdev 2015-05-07 09:03:19

4

我意識到這個問題是比較舊的(2011)添加此,但只是爲了發佈一個最新的答案,以防某人(像我)搜索這個問題,並在這個問題上絆倒:

在iOS 9中,Apple改進了很多UITableView。爲了確保當一個表被重載的重點不丟失,只是這樣做:

tableView.remembersLastFocusedIndexPath = Yes; 

,它會「神奇的工作」。

更妙的是,實施

- (NSIndexPath *)indexPathForPreferredFocusedViewInTableView:(UITableView *)tableView 

UITableViewDelegate讓表知道什麼時候被吸引到篩選的第一次你想選擇哪一行。

在iOS 9之前,我通常會編寫我自己的reloadData方法,即獲取當前選定的行,重新載入表格然後再次選擇它。無論是在UITableViewController,或者如果我需要那個表,我不能防止代碼直接調用reloadData,我分類UITableView和覆蓋reloadData(嘿,委派是首選的方式,但有時你只需要繼承和重寫,即使在iOS中)。

0

使用以下代碼獲取表視圖中的選定行。 :)

NSArray *selectedRows=[tableView indexPathsForSelectedRows]; 
     NSMutableArray *rownumberArray=[[NSMutableArray alloc]init]; 
     for (int i=0; i<selectedRows.count; i++) { 
      NSIndexPath *indexPath = [selectedRows objectAtIndex:i]; 
      NSInteger row = indexPath.row; 
      NSNumber *number = [NSNumber numberWithInteger:row]; 
      [rownumberArray addObject:number]; 
     } 

// rownumberArray包含選定單元格的行號。

+0

你可以使用'for(在selectedRows中的NSIndexPath * ip)來簡化這一點[rowNumberArray addObject:@(ip.row)];' – Koen 2017-04-18 17:42:56

相關問題