1

我正在使用 iPad 中的 SplitViewController 处理 MasterDetail 视图,我的主视图有一个 tableView,我在其中动态添加行。因此,新添加的行位于零位置。所以我想要实现的是,我想在新添加的行添加到主表后立即选择它。稍后当用户添加新行时,应选择该行并取消选择先前添加的行。

为此,我编写了下面的代码

- (void)selectNewlyAddedRow
{
            NSIndexPath* selectedCellIndexPath= [NSIndexPath indexPathForRow:0 inSection:0];
        [m_tableView selectRowAtIndexPath:selectedCellIndexPath animated:TRUE scrollPosition:UITableViewScrollPositionNone];
        [self tableView:m_tableView didSelectRowAtIndexPath:selectedCellIndexPath];
}

如果我在函数 ViewDidAppear 中编写此代码有效,但如果我在 cellForRowAtIndexPath 中编写它,则它不起作用..

请让我正确理解我哪里出错了。

问候兰吉特

4

2 回答 2

0

当您添加新行时,viewDidAppear 不会被调用,因此您只需注册表重新加载通知并在该通知调用的方法中选择第一行我希望它会对您有所帮助....

   - (void)selectFirstRow{

        if ([self.tableView numberOfSections] > 0 && [self.tableView numberOfRowsInSection:0] > 0) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
            [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
            [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
        }
    }
于 2013-05-10T09:15:56.733 回答
0

实现这个委托 -

   -(void)tableView:(UITableView )tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath)indexPath{ 
          [NSNotificationCenter defaultCenter]; 
          [notificationCenter postNotificationName:@"Table Reloaded" object:nil];
    } 

在您的视图控制器中,在 viewdidload 中添加此行

   -(void)viewDidLoad{ 
          [super viewDidLoad];
           [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tableReloaded:) name:@"Table Reloaded" object:nil];
   }


  -(void) tableReloaded:(NSNotification*)notification{ 
        [self selectFirstRow];
  }
于 2013-05-10T09:41:05.327 回答