在initWithNibName中获取plist文件中的内容
1 // 获取plist文件 2 NSDictionary * dic = [NSDictionary dictionaryWithContentsOfFile:@"plist文件地址"]; 3 NSLog(@"%@",dic); 4 self.BookDic = [NSMutableDictionary dictionary]; 5 // 将文件中的所有内容都取出 6 for (NSString * key in dic) { 7 NSArray * arr = [dic objectForKey:key]; 8 self.bookArr = [NSMutableArray array]; 9 for (NSDictionary * temDic in arr) { 10 Books * book = [[Books alloc]init]; 11 book.name = [temDic objectForKey:@"name"]; 12 book.content = [temDic objectForKey:@"content"]; 13 book.img = [temDic objectForKey:@"photo"]; 14 [self.bookArr addObject:book]; 15 16 [book release]; 17 NSLog(@"%@",book.name); 18 19 } 20 [self.BookDic setObject:self.bookArr forKey:key]; 21 } 22 self.view.backgroundColor = [UIColor cyanColor];
设置section的个数
1 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 2 3 // 设置section的个数 4 return self.BookDic.allKeys.count; 5 }
设置row的个数
1 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 2 3 // 设置每个section的行数 4 NSString *key = [[self.BookDic allKeys] objectAtIndex:section]; 5 return [[self.BookDic objectForKey:key] count]; 6 }
对cell进行传值
1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 2 3 BookTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"bookCell"]; 4 5 if (!cell) { 6 cell = [[BookTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"bookCell"]; 7 } 8 9 // 进行传值 10 NSString *key = [[self.BookDic allKeys] objectAtIndex:indexPath.section]; 11 NSMutableArray *arr = [self.BookDic objectForKey:key]; 12 13 Books * bok = [arr objectAtIndex:indexPath.row]; 14 cell.book = bok; 15 // Configure the cell... 16 17 return cell; 18 }
时间: 2024-10-23 20:27:01