【发布时间】:2026-01-01 20:30:01
【问题描述】:
我是新手,所以代码中可能有很多错误。问题是。当我单击单元格打开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>
帮帮我!
【问题讨论】:
-
在推动视图控制器显示文本“Hello world”之前是否设置
titleGet像“Hello world”这样的常量值?请在 'DetailsViewController' 内的viewDidLoad中添加日志以供进一步检查。 -
我觉得inn这个概念不需要Plist,你可以直接把字段移到下一页
-
@Anbu.Karthik 怎么办?
标签: ios objective-c xcode uiviewcontroller plist