我正在试图从URL加载plist文件。我不确定是否我的代码设置导致它崩溃或者如果我做错了什么。从URL而非文件加载plist
.m文件
#import "deptTableViewController.h"
@interface deptTableViewController()
@property (nonatomic, copy) NSDictionary *names;
@property (nonatomic, copy) NSArray *keys;
@property (nonatomic, strong) NSMutableArray *filterednames;
@property (nonatomic, strong) UISearchDisplayController *searchController;
@end
@implementation deptTableViewController
@synthesize names, keys, filterednames, searchController, searchNames;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UITableView *tableview = (id) [self.view viewWithTag:1];
[tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
filterednames = [[NSMutableArray alloc]init];
searchController = [[UISearchDisplayController alloc]init];
searchController.searchResultsDataSource = self;
// This will get the plist into data format
NSData *dataReturn = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://morphinggamers.ca/staff.plist"]];
NSString *path = [[NSBundle mainBundle]pathForResource:@"staff" ofType:@"plist"];
names = [NSDictionary dictionaryWithContentsOfFile:path];
keys = [[names allKeys]sortedArrayUsingSelector:@selector(compare:)];
keys = [NSKeyedUnarchiver unarchiveObjectWithData:dataReturn];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView.tag == 1) {
return [keys count];
}
else{
return 1;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (tableView.tag == 1) {
NSString *key = keys[section];
NSArray *keyValues = names[key];
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];
// Configure the cell...
if (tableView.tag == 1) {
names = keys[indexPath.row];
NSString *teacherNames = names[@"teacherNames"];
cell.textLabel.text = teacherNames;
}
else{
cell.textLabel.text = filterednames [indexPath.row];
}
return cell;
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if (tableView.tag == 1) {
return keys[section];
}
else{
return nil;
}
}
#pragma mark - Search Display And Delegate Methods
-(void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView {
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
[filterednames removeAllObjects];
if (searchString.length > 0) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [search] %@", self.searchNames.text];
for (NSString *key in keys) {
NSArray *matches = [names[key]filteredArrayUsingPredicate:predicate];
[filterednames addObjectsFromArray:matches];
}
}
return YES;
}
@end
你得到什么错误?你还试图解决这个问题吗? – RyanR 2014-09-22 18:23:10
我得到一个(线程1:信号SIGABRT)。我没有尝试太多。林不知道是否我编码的表视图加载不同的部分导致它。 – macguy3 2014-09-22 18:24:55
你的空间*真的*需要帮助。提交了一个编辑。一般来说,请让您的问题易于阅读,以便人们不必滚动浏览3页空格。 – rebello95 2014-09-22 18:25:00