繁体   English   中英

DetailViewController不显示详细信息

[英]DetailViewController don't show detail

我是新手,因此代码中可能有很多错误。 问题是当我单击单元格时打开detailviewcontroller.but不显示我在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. 未启动arrayPlace
  2. 由于“ copy”指令,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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM