如何在 segue 期间从视图控制器引用 tableView

发布于 2024-12-12 04:47:25 字数 1383 浏览 0 评论 0 原文

我是 iOS 开发新手,所以请对我轻松点:)

使用适用于 iOS 5 的 Xcode 4.2,我在 IB 中构建了一个视图控制器,其中的 tableview 通过推送 Segue 链接到详细视图控制器(位于表视图单元格级别)

我想根据所选单元格的索引使用数据源中的数据设置详细信息页面标签。最初我编写了“didSelectRowAtIndexPath”只是为了发现这是在segue处理之后调用的,并发现我需要调用prepareForSegue而不是

我的问题是,无论我尝试什么,因为tableView不是传递给prepareForSegue的参数(与到目前为止我使用过的大多数其他方法),我正在努力访问视图控制器中的 tableView 以便调用 indexPath 方法,这样我就可以引用我的数据源

我已经看到了很多例子使用 self,如下所示,但这会导致错误,指出视图控制器中不存在 tableView

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

  NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
  DetailViewController *detail = [segue destinationViewController];
  detail.primary = [datasource objectAtIndex:selectedIndexPath.row];
  detail.seconday = [datalist objectForKey:detail.primary];
}

我也尝试过使用

ViewController *main = [segue viewController];

而不是使用 self,但这会导致同样的问题,

我知道我可以将示例重写为改为表视图控制器,但想知道这种方式是否可行?正如我看到人们在开发中使用这两种方法

作为参考,我正在遵循本教程... http://kurrytran.blogspot.com/2011/10/ios-5-storyboard-and.html

当我到达完成详细信息页面的部分时,我想,为什么不使用segue将表视图单元格链接到详细信息页面,并在显示详细信息页面之前对州和首都的复制进行编码。正如您从本教程中看到的,主控制器是视图控制器

我的理解是,不使用 UITableViewController 会给任何应用程序更大的灵活性,如果它需要稍后发展成更复杂的东西

希望你能提供帮助, 伊兹

I'm new to iOS development, so go easy on me please :)

Using Xcode 4.2 for iOS 5, I've built a view controller in IB, with a tableview inside it linked to a detail view controller through a push segue (at table view cell level)

I'm wanting to setup the detail pages labels with data from my datasource based on the index of the cell selected. Initially I coded the "didSelectRowAtIndexPath" only to find out that this is called after the segue processing, and found out that I need to call prepareForSegue instead

My problem is, that whatever I try, as tableView is not a passed parameter to prepareForSegue (unlike most other methods I've used thus far), I'm struggling to get access to the tableView within the view controller in order to call the indexPath method, such that I can reference my datasource

I've seen many examples using self, as below, but this results in an error stating that tableView doesn't exist within the view controller

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

  NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
  DetailViewController *detail = [segue destinationViewController];
  detail.primary = [datasource objectAtIndex:selectedIndexPath.row];
  detail.seconday = [datalist objectForKey:detail.primary];
}

I've also tried using

ViewController *main = [segue viewController];

Instead of using self, but this results in the same issue

I know that I could rewrite the example to be a Table View Controller instead, but wanted to know if it was possible this way ? As I've seen people use both methods in development

For reference, I was following this tutorial ... http://kurrytran.blogspot.com/2011/10/ios-5-storyboard-and.html

When I got to the section for completing the detail page, I thought, why not use a segue to link the table view cell to the detail page, and code up the copying of the state and capital prior to the detail page being displayed. As you can see from this tutorial, the main controller is a view controller

My understanding was that NOT using an UITableViewController would give any app greater flexibility should it need to develop into something more complex later on

Hope you can help,
Izzy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深巷少女 2024-12-19 04:47:25

我也是 iOS 新手,但就其价值而言:

听起来您需要在 UIViewControllerClass 中创建一个指向表视图对象的指针。

@interface YourClass : UIViewController
{
    IBOutlet UITableView  *MyTableView;
}
@property ... IBOutlet UITableView *MyTableView;

...然后将其连接到 IB 中的“表视图”。这样您就可以在prepareForSegue 中访问UITableView 对象。

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSIndexPath *selectedIndexPath = [self.MyTableView indexPathForSelectedRow];
    ...
}

I'm also new to iOS, but for what it's worth:

Sounds like you need to create a pointer to your table view object in your UIViewControllerClass.

@interface YourClass : UIViewController
{
    IBOutlet UITableView  *MyTableView;
}
@property ... IBOutlet UITableView *MyTableView;

...and then hook this up to your "table view" within IB. Such that in prepareForSegue you can then access the UITableView object.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSIndexPath *selectedIndexPath = [self.MyTableView indexPathForSelectedRow];
    ...
}
嗼ふ静 2024-12-19 04:47:25

SWIFT 2 更新

我将 tableView “连接”为 ViewController 中情节提要的出口。

像这样: @IBOutlet weak var tableView: UITableView!

只需将 tableView 拖到 ViewController 中并使其成为一个插座即可。然后您可以引用它的属性,例如 self.tableView.reloadData()。

这个属性以及引用 tableView 的其他属性给我带来了错误,直到我将 tableView 添加为一个参考插座。

将其添加到您的班级中:
class TableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate

现在,indexPath.row 以及 segues 应该可以工作了。您还可以像这样使用 instantiateViewControllerWithIdentifier

  func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        let myOtherView = self.storyboard!.instantiateViewControllerWithIdentifier("otherViewController") as! otherViewViewController


            myOtherView.url = NSURL(string:"\(array[indexPath.row])")

            self.presentViewController(myOtherView, animated: true, completion: nil)


    }

希望有帮助。

SWIFT 2 UPDATE

I "hooked up" the tableView as an outlet from the storyboard in the ViewController.

like this: @IBOutlet weak var tableView: UITableView!

Just drag your tableView into the ViewController and make it an outlet. Then you can reference its properties such as self.tableView.reloadData().

This property as well as others that referenced the tableView were giving me errors until I added the tableView as a referencing outlet.

Add this to your class:
class TableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate

The indexPath.row should now work, as well as the segues. You can also use instantiateViewControllerWithIdentifier like this:

  func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        let myOtherView = self.storyboard!.instantiateViewControllerWithIdentifier("otherViewController") as! otherViewViewController


            myOtherView.url = NSURL(string:"\(array[indexPath.row])")

            self.presentViewController(myOtherView, animated: true, completion: nil)


    }

Hope that helps.

倒数 2024-12-19 04:47:25

我也是 iOS 新手,但值得一提的是:

NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];

prepareForSegue 中为我工作。谢谢,我试图弄清楚如何获取当前选定的行...

我不确定你的意思是“我在IB中构建了一个视图控制器,里面有一个tableview”,但这听起来像你的segue源是 UIViewController,而不是 UITableViewController?您的类需要子类化 UITableViewController 才能使 tableView 存在。

I'm also new to iOS, but for what it's worth this:

NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];

worked for me in prepareForSegue. Thanks, I was trying to figure out how to get the currently selected row...

I'm not sure what you mean by "I've built a view controller in IB, with a tableview inside", but it sounds like your segue source is a UIViewController, not a UITableViewController? Your class needs to subclass UITableViewController for tableView to exist.

街道布景 2024-12-19 04:47:25

After:

DetailViewController *detail = [segue destinationViewController];

put in:

[detail view];

then:

detail.primary = [datasource objectAtIndex:selectedIndexPath.row];

detail.seconday = [datalist objectForKey:detail.primary];

出于某种原因,仅设置 segue 不足以正确设置所有属性。您必须在destinationViewController 上添加呼叫视图。

After:

DetailViewController *detail = [segue destinationViewController];

put in:

[detail view];

then:

detail.primary = [datasource objectAtIndex:selectedIndexPath.row];

detail.seconday = [datalist objectForKey:detail.primary];

For some reason, just setting the segue isn't enough to get all the properties set up right. You have to add call view on your destinationViewController.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文