[英]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. 首先,将
delegate
和datasource
设置为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
. 设置
delegate
和datasource
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.