1

私は xcode にかなり慣れていないので、このタスクの実行に問題があります。クリックされたセルの対応する名前を示す UILabel を持つ詳細ビューに渡される名前を持つ検索バーを持つテーブルを作成しました。検索バーが機能し、結果をフィルタリングします。このチュートリアルを使用して、それを支援しました。

http://www.appcoda.com/how-to-add-search-bar-uitableview/

今、各セルに対応するUILabelの代わりに詳細ビューに画像を表示したいのですが、その方法がわかりません。ここに私が取り組んでいるコードがあります:

TableViewController.h:

@interface SearchViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) IBOutlet UITableView *tableView;

TableViewController.m:

@interface SearchViewController ()

@end


@implementation SearchViewController {
    NSArray *cards;
    NSArray *searchResults;}

@synthesize tableView = _tableView;
-(void)viewDidLoad
{
    [super viewDidLoad];
    cards = [NSArray arrayWithObjects:
             @"Snivy",
             @"Servine",
             @"Serperior",
             @"Tepig",
             @"Pignite",
             @"Emboar",
             @"Oshawott",
             @"Dewott",
             @"Samurott", nil];

}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate
                                    predicateWithFormat:@"SELF contains[cd] %@",
                                    searchText];

    searchResults = [cards filteredArrayUsingPredicate:resultPredicate];}


-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;
}
- (void)viewDidUnload
{
    [super viewDidUnload];
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [searchResults count];

    } else {
        return [cards count];
    }

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SearchCardCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
    } else {
        cell.textLabel.text = [cards objectAtIndex:indexPath.row];
    }



    return cell;}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"ShowSearchCard"]) {
        SearchCardViewController *destViewController = segue.destinationViewController;

        NSIndexPath *indexPath = nil;

        if ([self.searchDisplayController isActive]) {
            indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
            destViewController.cardName = [searchResults objectAtIndex:indexPath.row];

        } else {
            indexPath = [self.tableView indexPathForSelectedRow];
            destViewController.cardName = [cards objectAtIndex:indexPath.row];
        }
    }

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        [self performSegueWithIdentifier: @"ShowSearchCard" sender: self];
    }
}

UIViewController.h:

@property (strong, nonatomic) IBOutlet UILabel *cardLabel;
@property (strong, nonatomic) NSString *cardName;
@property (strong, nonatomic) NSArray *searchCardDetail;

UIViewController.m:

@implementation SearchCardViewController

@synthesize cardLabel;
@synthesize cardName;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    cardLabel.text = cardName;

    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {

    [super viewDidUnload];
}

つまり、「カード」はテーブル内の名前です。現在、カードの名前の UILabel にセグエします。代わりに、通常のテーブルとフィルター処理されたテーブルのカードの対応する画像にセグエしたいと思います。あなたの時間と助けに感謝します! ありがとう!

4

1 に答える 1