宽容他人,放过自己。

plist文件的实现

Posted on By anchoriteFili

在initWithNibName中获取plist文件中的内容

//        获取plist文件
        NSDictionary * dic = [NSDictionary dictionaryWithContentsOfFile:@"/Users/hehe/Plist/Plist/BooksList.plist"];
        NSLog(@"%@",dic);
        self.BookDic = [NSMutableDictionary dictionary];
//        将文件中的所有内容都取出
        for (NSString * key  in dic) {
            NSArray * arr = [dic objectForKey:key];
            self.bookArr = [NSMutableArray array];
            for (NSDictionary * temDic in arr) {
                Books * book = [[Books alloc]init];
                book.name = [temDic objectForKey:@"name"];
                book.content = [temDic objectForKey:@"content"];
                book.img = [temDic objectForKey:@"photo"];
                [self.bookArr addObject:book];
                
                [book release];
                NSLog(@"%@",book.name);
                
            }
            [self.BookDic setObject:self.bookArr forKey:key];
        }
        self.view.backgroundColor = [UIColor cyanColor];

设置section的个数

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

//    设置section的个数
    return self.BookDic.allKeys.count;
}

设置row的个数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  
//    设置每个section的行数
    NSString *key = [[self.BookDic allKeys] objectAtIndex:section];
    return [[self.BookDic objectForKey:key] count];
}

向cell中传值

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BookTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"bookCell"];
    
    if (!cell) {
        cell = [[BookTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"bookCell"];
    }
    
//    进行传值
    NSString *key = [[self.BookDic allKeys] objectAtIndex:indexPath.section];
    NSMutableArray *arr = [self.BookDic objectForKey:key];
    
    Books * bok = [arr objectAtIndex:indexPath.row];
    cell.book = bok;
    // Configure the cell...
    
    return cell;
}