[IOS开发进阶与实战]第二天:使用CoreData 在TabelView中添加实体的思考2

接上篇

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];
    
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值