1

私は初心者なので、ストーリーボードでXcode 4.3.2を使用しているので、コードの知識はあまりよくありません。これまで、データを含むテーブルビューにリンクするメインビューコントローラーによって作成しました。ただし、これを新しいView Controllerにリンクする方法がわかりません。つまり、新しいView Controllerをストーリーボードページにドラッグした場合、テーブルビューのセルを選択して、詳細を含む新しいページを開きます。チュートリアルの助けを借りたこれまでの私のコードは次のとおりです:-

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

私はアプリの残りの部分をこのビットに固執し、さまざまなコードを調べたチュートリアルを検索して快適に作業できますが、私の知識は非常に限られているため、ダミーガイドが必要です。素晴らしい。

みんなに感謝し、助けていただければ幸いです。

4

1 に答える 1

0

さて、どの行が選択されているかに関係なく、同じペン先を使用して情報を表示すると仮定します。まず、「Views」フォルダを右クリックまたはCtrlキーを押しながらクリックして、「NewFile」を選択します。.xibを使用して新しいUIViewControllerクラスを作成します。私のViewControllerには、ラベル(車の名前用)、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];
}

これは非常に基本的な実装ですが、プロセスが明確になることを願っています。辞書キーと画像名に単純な命名規則を設けることで、作業がはるかに簡単になります。

于 2012-04-12T14:45:12.270 に答える