2014-09-22 75 views
-1

我正在试图从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 
+0

你得到什么错误?你还试图解决这个问题吗? – RyanR 2014-09-22 18:23:10

+0

我得到一个(线程1:信号SIGABRT)。我没有尝试太多。林不知道是否我编码的表视图加载不同的部分导致它。 – macguy3 2014-09-22 18:24:55

+0

你的空间*真的*需要帮助。提交了一个编辑。一般来说,请让您的问题易于阅读,以便人们不必滚动浏览3页空格。 – rebello95 2014-09-22 18:25:00

回答

1

,所以你需要使用一个网络请求来从数据只能用initwithcontentsofurl使用本地文件(设备上的文件的话)这个URL是一个服务器上服务器。

NSURLRequest *request = [NSURLRequest requestWithURL:@"http://morphinggamers.ca/staff.plist"]; 
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 
    NSDictionary *names = [NSPropertyListSerialization propertyListFromData:data mutabilityOption:0 format:0 errorDescription:nil]; 

    keys = [[names allKeys]sortedArrayUsingSelector:@selector(compare:)]; 

    keys = [NSKeyedUnarchiver unarchiveObjectWithData:dataReturn]; 
    //might want to reload the tableview 
    [self.tableview reloadData] 
}]; 
+0

好的,我添加了它,然后回来时出现错误和警告。线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x7c8) – macguy3 2014-09-22 18:44:52

+0

代码还有一些其他问题,但对我来说,似乎加载plist是主要的误解(虽然我可以明白为什么你会这么想)NSURL是用于本地和远程URL)大部分仍然是正确实施tableview的样板代码 – devgeek 2014-09-23 16:47:02

+0

我不知道我为什么这么想。无论出于什么原因,我都没有把它放在一起。你还看到了什么其他问题? – macguy3 2014-09-23 17:17:15