【问题标题】:How to select a cell in a tableview to open a new view controller?如何在表格视图中选择一个单元格来打开一个新的视图控制器?
【发布时间】:2012-04-11 10:02:23
【问题描述】:

我使用带有故事板的 Xcode 4.3.2,因为我是新手,所以代码知识不是很好。到目前为止,我已经由主视图控制器创建,它链接到包含数据的表视图。但是我不知道如何将它链接到一个新的视图控制器,即如果我将一个新的视图控制器拖到情节提要页面上,我希望能够选择表格视图中的单元格,然后打开一个包含详细信息的新页面。到目前为止,在教程的帮助下我的代码是:-

Viewcontroller.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITabBarDelegate, UITableViewDataSource, UISearchBarDelegate>
    {
    IBOutlet UITableView * tableView;
    IBOutlet UISearchBar * searchBar;

        NSArray * allItems;
        NSMutableArray * displayItems;
}

@end

Viewcontroller.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    allItems = [[NSArray alloc] initWithObjects:@"K12", @"K14", @"K16", @"K18", @"K42", @"K43", @"K46", @"K53", @"K60", @"K68", @"K81", @"K83", nil];
    displayItems = [[NSMutableArray alloc] initWithArray:allItems];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return  1;    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [displayItems count];
}

- (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }
    cell.textLabel.text = [displayItems objectAtIndex:indexPath.row];
    return cell;
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    if ([searchText length] == 0) {
        [displayItems removeAllObjects];
        [displayItems addObjectsFromArray:allItems];
    } else {
        //here
        [displayItems removeAllObjects];
        for (NSString * string in allItems) {
            NSRange r = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];
            if (r.location != NSNotFound) {
                [displayItems addObject:string];
        }
    }
}

    [tableView reloadData];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)aSearchBar {
    [searchBar resignFirstResponder];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end

我觉得解决应用程序的其余部分只是停留在这一点上并搜索教程查看了各种代码但由于我的知识非常有限我只需要一个傻瓜指南或者如果有人可以告诉我要添加什么代码更好那会很棒。

谢谢大家,我们将不胜感激。

【问题讨论】:

  • 您希望根据所选 textLabel 的内容,还是根据所选行来呈现不同的 viewController?看起来行数会有所不同,它们的内容也会有所不同,基于搜索,这会使事情变得有点复杂。仅供参考,不确定在 Storyboard 中是否可行——您很可能必须使用笔尖。
  • 另外,这取决于你是否真的需要为不同的选择使用不同的viewControllers,或者你是否可以使用同一个viewController,显示不同的数据。
  • 感谢您的回复。是的,每个选定的行都将包含新视图上的不同数据,因此如果我选择“K12”,那么 K12 的信息将显示其自己的视图/页面。例如,如果它被标记为“福特汽车”,那么当您单击它时,它将显示车辆详细信息。至于笔尖,我不确定如何在 4.3.2 Xcode 中添加笔尖文件。我有点希望将一个新的视图控制器拖到情节提要上,然后以某种方式链接到它,但是如果我必须使用 nib 文件,那么任何帮助将不胜感激。再次感谢您的时间。
  • 如果可能的话,我可以根据选择的单元格使用相同的视图控制器来显示不同的数据

标签: xcode4.2


【解决方案1】:

好的,我假设您将使用相同的笔尖来显示信息,而不管选择了哪一行。首先,右键单击或按住 ctrl 单击“视图”文件夹,然后选择“新建文件”。使用 .xib 创建一个新的 UIViewController 类。我的视图控制器将有一个标签(用于汽车的名称)、一个 UIImageView(汽车的,由与汽车同名的图像填充,带有 .jpg 附录)和一个由字典填充的 textView . textView 将显示有关汽车的信息,我已经预先加载了包含该信息的字典,键入汽车的名称,如下所示:

[dictionary setObject:@"This is a car." forKey:@"Ford Taurus"];

字典是一个单例,所以每个类都可以访问它(请参阅https://stackoverflow.com/a/10093449/542400 了解如何实现它)。现在我有了新的 viewController(我们称之为“NewVC”),带有一个合成字符串(所以我的第一个 viewController 可以访问它),我将确保我的第一个 viewController 会在选择一行时做出响应:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NewVC *new = [[NewVC alloc] init];
    //assuming you have no array from which you populated your table
    new.myString = [[[myTable cellForRowAtIndexPath:indexPath] textLabel] text];

    //assuming you have a navigationController
    [self.navigationController pushViewController:new animated:YES]; 
}

现在在我的 NewVC 的 -viewWillAppear 方法中,我实现了以下内容:

-(void)viewWillAppear:(BOOL)animated{
    [myLabel setText:myString];
    NSString *string = [NSString stringWithFormat:@"%@.jpg", myString];
    UIImage *image = [UIImage imageNamed:string];
    [myImageView setImage:image];
    //see above for sharedDictionary info
    [myTextView setText: [[MainDictionary sharedDictionary]getStringForKey:myString]];
    [super viewWillAppear:YES];
}

这是一个非常基本的实现,但我希望它能使过程清晰。通过为字典键和图像名称建立一个简单的命名约定,您的工作将会轻松很多。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多