简体   繁体   English

从UITableView(不是UITableViewController)转到UIViewController

[英]Go from UITableView(not a UITableViewController) to a UIViewController

I have a UITableView inside a UIViewController . 我在UIViewController有一个UITableView
When the user presses a cell inside the UITableView I would like to show another UIViewController . 当用户按下UITableView内部的单元格时,我想显示另一个UIViewController So I have UIViewController "A", with UITableView "A" inside and I would like to click a cell in UITableView "A" and be pushed to UIViewController "B". 所以我有UIViewController “ A”,里面有UITableView “ A”,我想单击UITableView “ A”中的一个单元格,然后将其推到UIViewController “ B”。 I tried simply connecting a segue from the UITableViewCell to the UIViewController but this results in nothing happening when the cell is pressed. 我尝试仅将一个序列从UITableViewCell连接到UIViewController但这在按下单元格时什么也没有发生。 I also tried the following code unsuccessfully. 我也尝试了以下代码失败。 Again, nothing happening, no errors. 同样,什么也没有发生,没有错误。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
    AlbumsViewController *albumsViewControllerObj = [mainStoryboard instantiateViewControllerWithIdentifier:@"AlbumsViewController"];
   [albumsViewControllerObj performSegueWithIdentifier:@"showAlbumDetail" sender:self];

}

All of the advice I have found so far involves going from a Table View Controller to another view, which I know how to do and have never had a problem with. 到目前为止,我发现的所有建议都涉及从Table View Controller转到另一个视图,我知道该怎么做并且从未遇到过问题。 For some reason though I am having issues moving from a Table View to another View Controller. 由于某种原因,尽管我在从表视图移动到另一个视图控制器时遇到问题。 If it matters (and I think it may) the delegate and datasource of the TableView "A" is View Controller "A". 如果很重要(我认为可能如此),则TableView“ A”的委托和数据源是视图控制器“ A”。 Any advice would be great, thank you! 任何建议都很好,谢谢!

First,set the delegate and datasource to self. 首先,将delegatedatasource设置为self。

Second,just drag a segue from ViewControllerA to ControllerB 其次,只需将一个序列从ViewControllerA拖到ControllerB

Third,just perform segue when select 第三,选择时只需执行搜索

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  [self  performSegueWithIdentifier:@"showAlbumDetail" sender:nil];
    }

Then,in prepareForSegue ,pass data to destinnatinVC 然后,在prepareForSegue ,将数据传递给destinnatinVC

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if (segue.identifier == yourid) {
        ViewControllerB *  = segue.destinationViewController;
        //Pass data
    }
}

Set up the delegate and datasource, either programmatically like this: 像下面这样以编程方式设置委托和数据源:

- (void)viewDidLoad {
    ...
    self.myTableView.delegate = self;
    self.myTableView.datasource = self;
}

Or you can do it in the storyboard by ctrl click and drag from your tableView to the viewController. 或者,您也可以通过在故事板上通过ctrl单击并从tableView拖动到viewController来实现。

Then you can segue to viewController b, programmatically or in the storyboard. 然后,您可以通过编程方式或在故事板上找到viewController b。

Porgrammatically: Porgrammatically:

First go to the storyboard and ctrl click and drag from viewController a to viewController b. 首先转到情节提要,然后按住Ctrl键单击,然后从viewController a拖动到viewController b。 Then in your code write the following: 然后在您的代码中编写以下内容:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self  performSegueWithIdentifier:@"nextViewControllerID" sender:nil];
}

StoryBoard: 故事板:

ctrl click and drag from the cell to viewController b. ctrl单击并从单元格拖动到viewController b。

Check first Any chance you accidentally typed didDeselectRowAtIndexPath ? 首先检查您是否偶然输入了didDeselectRowAtIndexPath吗? .Check out if the method name of what you expect of being didSelect may accidentally be gotten didDeselect in some way. 。检查出来,如果你希望成为什么样的方法名didSelect可能会意外地得到didDeselect以某种方式。

in the SimpleTableController.h SimpleTableController.h

#import <UIKit/UIKit.h>

@interface SimpleTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

@end

In SimpleTableViewController.m .check if didSelectRowAtIndexPath is called or not by doing as below SimpleTableViewController.m通过执行以下操作检查是否调用didSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UIAlertView *messageAlert = [[UIAlertView alloc]
                                        initWithTitle:@"Row Selected" message:@"You've selected a row" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

        // Display Alert Message
        [messageAlert show];

    }

set the delegate and datasource . 设置delegatedatasource

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将具有UITableView的UIViewController转换为UITableViewController - Transform UIViewController with UITableView into UITableViewController 如何以编程方式从UIViewController转到UITableViewController - How to programmatically go to a UITableViewController from UIViewController 从UITableViewController切换到具有单独的UITableView的UIViewController后,无法识别UITableView委托 - UITableView delegates not recognised after switching from UITableViewController to a UIViewController with separate UITableView 从UIViewController选择到UITableViewController - segue from UIViewController to UITableViewController 将标记从UITableViewController添加到UIViewController - Add markers from UITableViewController to UIViewController 从UITableViewController将NSFetchedResultsController迁移到UIViewController - Migrating NSFetchedResultsController to UIViewController from UITableViewController 将示例从UITableViewController切换到UITableView - Switch example from UITableViewController to UITableView 如何在从UITableViewController到UIViewController的展开中声明UITableViewController - How to declar UITableViewController in Unwind segue from UITableViewController to UIViewController Xcode:将信息从UITableViewController传递到UIViewController - Xcode: Passing Information from UITableViewController to UIViewController 如何从一个UITableViewController回调到另一个UIViewController - How to make a callback from a UITableViewController to another UIViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM