How to reference tableView from a view controller during a segue

前端 未结 4 603
旧时难觅i
旧时难觅i 2020-12-15 11:35

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

相关标签:
4条回答
  • 2020-12-15 11:57

    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.

    0 讨论(0)
  • 2020-12-15 11:59

    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.

    0 讨论(0)
  • 2020-12-15 12:08

    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];
        ...
    }
    
    0 讨论(0)
  • 2020-12-15 12:12

    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.

    0 讨论(0)
提交回复
热议问题