Я новичок в xcode, поэтому у меня проблемы с выполнением этой задачи. Я создал таблицу с панелью поиска с именами, которые передаются в подробное представление с UILabel, который показывает соответствующее имя выбранной ячейки. Панель поиска работает и фильтрует результаты. Я использовал этот учебник, чтобы помочь мне с этим:
http://www.appcoda.com/how-to-add-search-bar-uitableview/
Теперь я хочу иметь изображение в подробном представлении вместо UILabel, которое соответствует каждой из ячеек, но мне трудно понять, как это сделать. Вот код, с которым я работаю:
Таблевиевконтроллер.h:
@interface SearchViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) IBOutlet UITableView *tableView;
Таблевиевконтроллер.м:
@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 имени карты, и я хотел бы, чтобы вместо этого он переходил к соответствующему изображению карты в обычной таблице и отфильтрованной таблице при поиске. Я ценю ваше время и помощь! Спасибо!