接上篇
2)看看我们每行中的数据多少
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
id<NSFetchedResultsSectionInfo>sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
NSLog(@"[sectionInfo numberOfObjects]%d",[sectionInfo numberOfObjects]);
return [sectionInfo numberOfObjects];
}
我们先看看 这个协议.大致意思是说 我们获得选择行的信息.然后返回每行中的所有信息的条数,当然,或者信息的方式还是通过resultsController来获得的.至于最后的index,还是依赖于我们的section行了
@protocol NSFetchedResultsSectionInfo
/* Name of the section
*/
@property (nonatomic, readonly) NSString *name;
/* Title of the section (used when displaying the index)
*/
@property (nonatomic, readonly) NSString *indexTitle;
/* Number of objects in section
*/
@property (nonatomic, readonly) NSUInteger numberOfObjects;
/* Returns the array of objects in the section.
*/
@property (nonatomic, readonly) NSArray *objects;
@end // NSFetchedResultsSectionInfo
3)大快人心的时候终于到了,我们要绘制 cell了,记得看看ios6 的更新哦
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSManagedObject *aHero = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSInteger tab = [self.heroTapBar.items indexOfObject:self.heroTapBar.selectedItem];
switch (tab) { //根据user不同的选择item,我们可以得到重点不同的数据.detail~cell,你懂的
case kByName:
cell.textLabel.text = [aHero valueForKey:@"name"];
cell.detailTextLabel.text = [aHero valueForKey:@"secretIdentify"];
break;
case kBySecretIdentify:
cell.textLabel.text = [aHero valueForKey:@"secretIdentify"];
cell.detailTextLabel.text = [aHero valueForKey:@"name"];
break;
}
// Configure the cell...
return cell;
}
6.关于 section的修改的协议实现
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObjectContext *managedObjectContext = [self.fetchedResultsController managedObjectContext];//首先就获得了我们需要的上下文,因为只有有了上下文,我们才能修改,这就相当于 总管~~~
if (editingStyle == UITableViewCellEditingStyleDelete) {//选择了删除按钮
// Delete the row from the data source
[managedObjectContext deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]]; //我们很简单的仅仅是根据 indexPath 就删除了对应的section,是不是很快?
NSError *error = nil;
if (![managedObjectContext save:&error]) {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"error deting entity" message:[NSString stringWithFormat:@"error was; %@,quitting", [error localizedDescription] ] delegate:self cancelButtonTitle:@"aw buts ~" otherButtonTitles:nil];
[alertView show];
}
}
}
7.TabBar 协议
//实现tabbar协议来实现 记忆
#pragma mark tabbar ptotocol method
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSUInteger tabIndex = [tabBar.items indexOfObject:item];
[defaults setInteger:tabIndex forKey:kSelectedtabDefaultsKey];
NSLog(@"存储成功 %d",tabIndex);
[NSFetchedResultsController deleteCacheWithName:@"Hero"];
_fetchedResultsController.delegate = nil;
_fetchedResultsController = nil;
NSError *error;
if (![self.fetchedResultsController performFetch:&error]) {
NSLog(@"error performfetch :%@",[error localizedDescription]);
}
[self.heroTableView reloadData];
}
7.addbutton
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
exit(-1);//如果出现了警告,alert消失之后直接正常退出
}
- (IBAction)addHero:(id)sender {
NSManagedObjectContext *managedObjectContext = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity =[[self.fetchedResultsController fetchRequest] entity];
[NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:managedObjectContext];//实体的增加
NSError *error;
if (![managedObjectContext save:&error]) {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"error saving entity" message:[NSString stringWithFormat:@"error was; %@,quitting", [error localizedDescription] ] delegate:self cancelButtonTitle:@"aw buts ~" otherButtonTitles:nil];
[alertView show];
}
}
//关于编辑的按钮的状态改变
-(void)setEditing:(BOOL) editing animated:(BOOL) animated
{
[super setEditing:editing animated:animated];
self.addButton.enabled = !editing;
[self.heroTableView setEditing:editing animated:animated];
}