2011-05-24 31 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

相关问题