3

在我的应用程序中,我有一个 ParentViewController,其中包含多个元素。其中一个元素是 UITableView。

我使用我在情节提要中设计的动态原型单元设计了表格的单元。

意思是,我在情节提要中有一个 UIViewController;在里面我有一个 UITableView;在 UITableView 我有一个原型单元格。

现在,我在 Prototype Cell 中添加了一个 UIButton,创建了另一个 UIViewController(我们称之为 ChildViewController),并将 Storyboard 中的 UIButton 和 ChildViewController 与 Segue 连接起来。

当用户单击 UIButton 时,我应该如何编写激活 Segue 的代码?

我设法创建了表本身并在动态单元格中有数据(它们在 UIButton 旁边有一些 UILabel),但我无法弄清楚如何将 UIButton 正确连接到代码。

我不想使用 UITableView 默认的“选择”功能,因为我希望在动态单元格中有几个按钮 - 每个按钮都会派生出不同的 segue 到不同的 ChildViewController。

4

1 回答 1

1

这是我解决相同问题的方法。希望它会有所帮助。

为此,您可以将指向 UITableViewController 的指针传递给自定义单元格的初始化方法,并将其存储在那里:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
            // ...
            CDTrackCell *cell = [tableView dequeueReusableCellWithIdentifier:strCellTableIdentifier];
            CDTrack *currTrack = [self.fetchedResultsController objectAtIndexPath:indexPath];

            [cell configureView:currTrack inTableViewController:self];

            return cell;
   }

将指针保存在自定义 UITableViewCell 类中:

- (void) configureView:(CDTrack*)track
 inTableViewController:(CDTracksViewController*)_tvc
{
    self.tvc = _tvc;
    // ...
}

然后,您可以在点击按钮时执行 segue:

- (IBAction)buttonAddToTracklistTapped:(UIButton *)sender {
    [self.tvc performSegueWithIdentifier:@"showAddToTracklist" sender:self];
}

有更好的想法吗?

于 2012-08-29T08:55:56.073 回答