English 中文(简体)
如何在一张桌上选择一个囚室,以开放一个新的观察控制器?
原标题:How to select a cell in a tableview to open a new view controller?
  • 时间:2012-04-11 10:02:23
  •  标签:
  • xcode4.2

采用X代码4.3.2,把故事板作为新版,因此,密码知识并不很好。 我迄今为止是由主要观点控制器创建的,这些控制器与我载有数据的表象相链接。 然而,我知道如何将这个内容与一个新的观点控制员联系起来,即如果把新的观点控制员拖到故事板上,那么我就希望能够选择桌面调查中的小组,然后打开一个包含细节的新页。 至今,在辅导员的帮助下,我的法典是:

View Controller.h

#import <UIKit/UIKit.h>

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

        NSArray * allItems;
        NSMutableArray * displayItems;
}

@end

View Controller.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

我感到宽慰的是,在座右边的那部分人看着各种法典,但因为我的知识非常有限,我只需要一张珍贵的指南,或者说如果有人能够告诉我什么法典来补充,那将是巨大的。

Thanks Guys and help would be appreciated.

问题回答

Okay, I will assume that you will use the same nib to display the information, regardless of which row is selected. First, right-click or ctrl-click on your "Views" folder, and select "New File". Create a new UIViewController class, with .xib. My view controller will have a label (for the name of the car), a UIImageView (of the car, populated by an image that has the same name as the car, with a .jpg appendix), and a textView populated by a dictionary. The textView will display information about the car, and I have pre-loaded the dictionary with that information, keyed to the name of the car, like so:

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

字典是单一州,因此每一类别都可以查阅(见https://stackoverflow.com/a/10093449/542400)。 如何执行。 现在,我有我的新的观点,即“新老会”和综合体(我的第一个看法是,主计长可以查阅),我将确保我的第一种看法,即选举一行时,主计长将作出回应:

- (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]; 
}

现在,在我的“新千禧年”项目中,我采用了以下方法:

-(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];
}

这是一个非常基本的执行情况,但我希望它将使这一进程明确无误。 通过为字典钥匙和图像名称制定简单的命名公约,你的工作将更加容易。





相关问题
Issue with compiling example code

I m trying to compile this sample code with XCode 4.2b, here s how my project looks like: Application type: Command Line Tool. When I m trying to build the project, I get the following list of errors:...

Storyboard Partial Cut remove

With the new xCode Storyboard function it is posible to switch the view with some prepared switch-modes like: Style: "Model" Transition: "Partial Cut" So I have tried to use the "Partial Cut", but ...

Xcode 4.2: What is the most effective method switching views?

I was thinking of games in particular, where there may be alot of views that need to be switched in and out. Could someone supply some sample xcode 4.2 code? Many tutorials have old xcode versions ...

热门标签