DetailViewController不显示详细信息

时间:2016-02-16 17:42:19

标签: ios objective-c xcode uiviewcontroller plist

我是新手,因此代码可能会出现很多错误。问题是。当我单击单元格打开detailviewcontroller时,但不显示我在plist中设置的细节。 并且不要显示任何错误。

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>

帮助我们!

1 个答案:

答案 0 :(得分:0)

它不起作用,因为:

  1. 未启动arrayPlace
  2. 由于&#34; copy&#34;而arrayPlace定义设置了一个数组而不是可变数组。指令
  3. 解决方案是修复但不能解决问题。更改didSelectRowAtIndexPath
  4. <强>解决方案

    #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>