2014-09-22 74 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