2016-02-16 3 views
0

Я новичок и поэтому может быть много ошибок в коде. Проблема в том, что. Когда я нажимаю кнопку открытия ячейки detailviewcontroller.but, не показывайте детали, которые я установил в plist. И не показывать ошибок.DetailViewController не отображается подробно

TableViewController.h:

@interface NamesTableViewController : UITableViewController <UISearchDisplayDelegate, UISearchBarDelegate> 


    @property (strong, nonatomic) IBOutlet UISearchBar *searchBar; 

    @end 

TableViewController.m:

#import "NamesTableViewController.h" 
#import "DetailViewController.h" 

@interface NamesTableViewController() 
@property (nonatomic, copy) NSDictionary *propertyList; 
@property (nonatomic, copy) NSArray *letters; 
@property (nonatomic, copy)NSMutableArray *filteredNames; 
@property (nonatomic, strong)UISearchController *searchController; 

@property (nonatomic, copy)NSMutableArray *arrayPlace; 


@end 

@implementation NamesTableViewController 

@synthesize propertyList, letters, filteredNames, searchController , arrayPlace; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UITableView *tableView = (id)[self.view viewWithTag:1]; 

    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"]; 

    filteredNames = [[NSMutableArray alloc]init]; 
    searchController = [[UISearchController alloc]init]; 


    self.searchController.searchResultsUpdater = self; 

    NSString *path = [[NSBundle mainBundle] pathForResource:@"names" ofType:@"plist"]; 
    self.propertyList = [NSDictionary dictionaryWithContentsOfFile:path]; 
    self.letters = [[self.propertyList allKeys] sortedArrayUsingSelector:@selector(compare:)]; 

} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    if (tableView.tag == 1){ 

     return self.letters.count; 

    }else { 
     return 1; 
    } 

    } 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    if (tableView.tag == 1) { 

     NSString *letter = self.letters[section]; 
     NSArray *keyValues = [self.propertyList[letter] allKeys]; 

     [arrayPlace addObject:letter]; 

     return keyValues.count; 
    } else { 


     return [filteredNames count]; 
    } 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 

    // Configure the cell... 

    if (tableView.tag == 1){ 

     NSString *letter = self.letters[indexPath.section];; 
     NSArray *keyValues = [[self.propertyList[letter] allKeys] sortedArrayUsingSelector:@selector(compare:)]; 
     cell.textLabel.text = keyValues[indexPath.row]; 
    } else{ 
     cell.textLabel.text = filteredNames[indexPath.row]; 
    } 

    return cell; 

} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    //[self performSegueWithIdentifier:@"pushDetail" sender:self]; 
    DetailViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"]; 
    // Push the view controller. 
    [self.navigationController pushViewController:vc animated:YES]; 

    [vc setDictionaryGeter:[arrayPlace objectAtIndex:indexPath.row]]; 

} 

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { 
    return self.letters; 
} 

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 

    if (tableView.tag == 1) { 
     return letters [section]; 
    } else { 
     return nil; 
    } 
} 

#pragma mark Search Display Delegate Methods 

-(void)searchDisplayController:(UISearchController *)controller didLoadSearchResultsTableView:(UITableView *)tableView { 
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"]; 
} 



-(BOOL)searchDisplayController:(UISearchController *)controller shouldReloadTableForSearchString:(NSString *)searchString 

{ 

    [filteredNames removeAllObjects]; 
    if (searchString.length > 0) { 
     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [search] %@", self.searchBar.text]; 

     for (NSString *letter in letters) { 
      NSArray *matches = [[self.propertyList[letter] allKeys]filteredArrayUsingPredicate:predicate]; 

      [filteredNames addObjectsFromArray:matches]; 

     } 

    } 

    return YES; 
} 


@end 

DetailViewController.h:

@interface DetailViewController : UIViewController 

@property (strong, nonatomic) IBOutlet UILabel *textPlace; 
@property (nonatomic) NSString *titleGet; 
@property (nonatomic) NSDictionary *dictionaryGeter; 

@end 

DetailViewController.m:

#import "DetailViewController.h" 

@interface DetailViewController() 

@end 

@implementation DetailViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    _titleGet = [_dictionaryGeter objectForKey:@"Company"]; 
    _textPlace.text = _titleGet; 
} 

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

@end 

И свойство списка:

<plist version="1.0"> 
<dict> 
    <key>A</key> 
    <dict> 
     <key>Azer Huseynov</key> 
     <dict> 
      <key>Company</key> 
      <string>test ac</string> 
      <key>Position</key> 
      <string>test ap</string> 
      <key>Email</key> 
      <string>test ae</string> 
      <key>Number</key> 
      <string>test an</string> 
      <key>Photo</key> 
      <string>test ai</string> 
     </dict> 
    </dict> 
    <key>B</key> 
    <dict> 
     <key>Bahadur Ojakverdiyev</key> 
     <dict> 
      <key>Company</key> 
      <string>test bc</string> 
      <key>Position</key> 
      <string>test bp</string> 
      <key>Email</key> 
      <string>test be</string> 
      <key>Number</key> 
      <string>test bn</string> 
      <key>Photo</key> 
      <string>test bi</string> 
     </dict> 
    </dict> 
</dict> 

Помогите мне, ребята!

+0

делает установки 'titleGet' постоянное значение, как "Привет мир", прежде чем толкая ViewController показывает текст" Привет мир" ? Пожалуйста, поместите журнал внутри 'viewDidLoad' внутри 'DetailsViewController' для дальнейшей проверки. –

+0

Я думаю, что эта концепция не нуждается в Plist, вы можете напрямую переместить поля на следующую страницу –

+0

@ Anbu.Karthik, как это сделать? –

ответ

0

Это не работает, потому что:

  1. arrayPlace не инициируется
  2. Определение arrayPlace установки массива вместо изменяемого массива из-за директивы «копировать»
  3. Решение исправление, но не разрешение проблемы. Изменение didSelectRowAtIndexPath

Решение

#import "NamesTableViewController.h" 
#import "DetailViewController.h" 

@interface NamesTableViewController() 

@property (nonatomic, copy) NSDictionary *propertyList; 
@property (nonatomic, copy) NSArray *letters; 
@property (nonatomic, copy)NSMutableArray *filteredNames; 
@property (nonatomic, strong)UISearchController *searchController; 

@property (strong, readwrite, nonatomic) NSMutableArray *arrayPlace; 


@end 

@implementation NamesTableViewController 

@synthesize propertyList, letters, filteredNames, searchController , arrayPlace; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.arrayPlace = [NSMutableArray new]; 

    UITableView *tableView = (id)[self.view viewWithTag:1]; 

    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"]; 

    filteredNames = [[NSMutableArray alloc]init]; 
    searchController = [[UISearchController alloc]init]; 


    self.searchController.searchResultsUpdater = self; 

    NSString *path = [[NSBundle mainBundle] pathForResource:@"names" ofType:@"plist"]; 
    self.propertyList = [NSDictionary dictionaryWithContentsOfFile:path]; 
    self.letters = [[self.propertyList allKeys] sortedArrayUsingSelector:@selector(compare:)]; 

} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    if (tableView.tag == 1){ 

     return self.letters.count; 

    }else { 
     return 1; 
    } 

} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    if (tableView.tag == 1) { 

     NSString *letter = self.letters[section]; 
     NSArray *keyValues = [self.propertyList[letter] allKeys]; 

     [arrayPlace addObject:letter]; 

     return keyValues.count; 
    } else { 


     return [filteredNames count]; 
    } 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 

    // Configure the cell... 

    if (tableView.tag == 1){ 

     NSString *letter = self.letters[indexPath.section];; 
     NSArray *keyValues = [[self.propertyList[letter] allKeys] sortedArrayUsingSelector:@selector(compare:)]; 
     cell.textLabel.text = keyValues[indexPath.row]; 
    } else{ 
     cell.textLabel.text = filteredNames[indexPath.row]; 
    } 

    return cell; 

} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 


    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

    NSString *keyTitle = cell.textLabel.text; 


    NSDictionary *peopleUnderLetter = [self.propertyList objectForKey:self.letters[indexPath.section]]; 

    __block NSDictionary *selectedPerson = nil; 

    [peopleUnderLetter enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) { 

     if ([key isEqualToString:keyTitle]) { 

      selectedPerson = obj; 

      *stop = YES; 

     } 

    }]; 

    if (selectedPerson) { 

     DetailViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"]; 
     // Push the view controller. 
     [self.navigationController pushViewController:vc animated:YES]; 

     [vc setDictionaryGeter:selectedPerson]; 
    } 

} 

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { 
    return self.letters; 
} 

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 

    if (tableView.tag == 1) { 
     return letters [section]; 
    } else { 
     return nil; 
    } 
} 

#pragma mark Search Display Delegate Methods 

-(void)searchDisplayController:(UISearchController *)controller didLoadSearchResultsTableView:(UITableView *)tableView { 
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"]; 
} 



-(BOOL)searchDisplayController:(UISearchController *)controller shouldReloadTableForSearchString:(NSString *)searchString 

{ 

    [filteredNames removeAllObjects]; 
    if (searchString.length > 0) { 
     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [search] %@", self.searchBar.text]; 

     for (NSString *letter in letters) { 
      NSArray *matches = [[self.propertyList[letter] allKeys]filteredArrayUsingPredicate:predicate]; 

      [filteredNames addObjectsFromArray:matches]; 

     } 

    } 

    return YES; 
} 

Plist файл

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
    <dict> 
     <key>A</key> 
     <dict> 
      <key>Azer Huseynov</key> 
      <dict> 
       <key>Company</key> 
       <string>test ac</string> 
       <key>Position</key> 
       <string>test ap</string> 
       <key>Email</key> 
       <string>test ae</string> 
       <key>Number</key> 
       <string>test an</string> 
       <key>Photo</key> 
       <string>test ai</string> 
      </dict> 
     </dict> 
     <key>B</key> 
     <dict> 
      <key>Bahadur Ojakverdiyev</key> 
      <dict> 
       <key>Company</key> 
       <string>test bc</string> 
       <key>Position</key> 
       <string>test bp</string> 
       <key>Email</key> 
       <string>test be</string> 
       <key>Number</key> 
       <string>test bn</string> 
       <key>Photo</key> 
       <string>test bi</string> 
      </dict> 
     </dict> 
    </dict> 
</plist> 
+0

Я изменился. Но dont working.gives error –

+0

Какая ошибка? и в каком методе? – Adobels

+0

Когда я нажимаю на сотрудника (пример: Азер Гусейнов) ... x код жалуется 'return UIApplicationMain (argc, argv, nil, NSStringFromClass ([AppDelegate class]);' thread 1: signal SIGABRT –

Смежные вопросы