iOS - 如何从 UITableViewCell 呈现不同的场景
Posted
技术标签:
【中文标题】iOS - 如何从 UITableViewCell 呈现不同的场景【英文标题】:iOS - How to present different scenes from UITableViewCell 【发布时间】:2015-05-30 11:40:34 【问题描述】:我有一个UIViewController
和一个TableView
。 TableView
填充了每个单元格的图像和文本。现在,当用户点击单元格时,我需要向其他人展示Scenes
。例如:
- 点击第一行 --> 呈现ViewController1
- 点击第二行 --> 呈现ViewController2
ViewController1
和 ViewController2
是我的Storyboard
中的场景。
我尝试了各种解决方案,但这些都不起作用。此外,当我点击一个单元格时,似乎没有调用 didSelectRowAtIndexPath:
方法(例如,我试图显示一个警报)。
这里是我的 ViewController 的代码: ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
NSArray *recipes;
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
recipes = [NSArray arrayWithObjects:@"News", @"Calendar",@"Take a photo",@"Draw",@"Mail us",@"Follow us on Facebook", nil];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return [recipes count];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *simpleTableIdentifier = @"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
cell.textLabel.text = [recipes objectAtIndex:indexPath.row];
if ([cell.textLabel.text isEqualToString:recipes[0]])
cell.imageView.image = [UIImage imageNamed:@"newsicon"];
else if ([cell.textLabel.text isEqualToString:recipes[1]])
cell.imageView.image = [UIImage imageNamed:@"mensaicon"];
else if ([cell.textLabel.text isEqualToString:recipes[2]])
cell.imageView.image = [UIImage imageNamed:@"fotoicon"];
else if ([cell.textLabel.text isEqualToString:recipes[3]])
cell.imageView.image = [UIImage imageNamed:@"drawicon"];
else if ([cell.textLabel.text isEqualToString:recipes[4]])
cell.imageView.image = [UIImage imageNamed:@"mailicon"];
else if ([cell.textLabel.text isEqualToString:recipes[5]])
cell.imageView.image = [UIImage imageNamed:@"fbicon"];
return cell;
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
// here we get the cell from the selected row.
UITableViewCell *selectedCell=[tableView cellForRowAtIndexPath:indexPath];
if ([selectedCell.textLabel.text isEqualToString:recipes[0]])
UIAlertView *messageAlert = [[UIAlertView alloc]
initWithTitle:@"Row Selected" message:@"You've selected a row" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
// Display Alert Message
[messageAlert show];
NSString *storyboardName = @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"news"];
[self presentViewController:vc animated:YES completion:nil];
// use the image in the next window using view controller instance of that view.
@end
我是 ios 开发新手,所以我不知道我的代码是否正确,或者是否有其他更优雅的解决方案。无论如何,让我们专注于这个问题,任何人都可以帮助我吗?
【问题讨论】:
您的didSelectedRowAtIndexPath
没有被调用,因为您在单元格上添加了图像。请设置 imageView cell.imageView.userInteractionEnabled= false 的属性。在此之后你的didSelectedRowAtIndexPath
打电话。同时设置 Delegate 和 DataSource 像 self.tabelView.delegate = self; self.tableView.datasource = self;
。
【参考方案1】:
如果您的视图只是一个表格视图,我建议使用 UITableViewController 而不是 UIViewController。如果您在现有 UIViewController 中有 UITableView,则需要自己设置委托和数据源方法。 您可以在情节提要中执行此操作。单击您的 tableView,然后选择连接检查器。然后单击 dataSource 旁边的圆圈并将其拖动到您的视图控制器。对委托做同样的事情。这可能就是您的表格视图方法没有被调用的原因。
如果是静态表格,您可以从情节提要中的每个单元格创建独立的转场。
【讨论】:
...捂脸。我知道,但我忘了将委托拖到视图控制器上。多么愚蠢的错误。谢谢你提醒我。可能这里的每个人都试图说出来,但你的回复让我明白了发生了什么。【参考方案2】:此外,当我点击一个单元格时,似乎没有调用方法 didSelectRowAtIndexPath: (例如,我试图显示一个警报)。
您的基础类是UIViewController
,因此,您的UITableViewDelegate
和UITableViewDataSource
不会自动设置。你需要自己做。例如,您可以在init
函数中设置它们:
- (instancetype)init
self = [super init];
if( self )
self.delegate = self;
self.datasource = self;
return self;
另一种方法是使用UITableViewController
作为您的基本调用,那么您无需担心设置委托。
【讨论】:
@Kurtis92,在 .m 文件中,您可以将它们放在 viewDidLoad 函数之前【参考方案3】:根据您的代码警报,仅当您单击第一个单元格时才会显示.. 但是根据您的需要,让我编写一个代码来帮助您..
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
// here we get the cell from the selected row.
NSString *selectedcelltext=[recipes objectAtIndex:indexPath.row];
UIAlertView *messageAlert = [[UIAlertView alloc]
initWithTitle:@"Row Selected" message:[NSString stringWithFormat:@"You've selected a %@ row ",selectedcelltext] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
// Display Alert Message
[messageAlert show];
if ([selectedcelltext isEqualToString:recipes[0]])
NSString *storyboardName = @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"news"];
[self presentViewController:vc animated:YES completion:nil];
else if ([selectedcelltext isEqualToString:recipes[1]])
NSString *storyboardName = @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"calendar"];
[self presentViewController:vc animated:YES completion:nil];
// use the image in the next window using view controller instance of that view.
【讨论】:
以上是关于iOS - 如何从 UITableViewCell 呈现不同的场景的主要内容,如果未能解决你的问题,请参考以下文章
如何从 UITableViewCell 按钮单击单独的 UIViewController 创建 UIView?
iOS Swift 如何更改或更新自定义 UITableViewCell 中的 UILabel 属性
如何获取uibutton(UITableViewCell的子视图)相对于整个窗口iOS Objective-C的位置?