2013-07-15 45 views
1

我是iOS编程新手,这是我创造的第一个应用程序。基本上我在制作这个应用程序时并不知道核心数据,所以我正常开发了这些类。我的应用程序基本上是一个待办事项列表应用程序,其中Tasks作为输入taskArray(作为TableViewController的数据源的数组)的对象。这些任务由用户输入到taskArray。从应用程序关闭时删除的对象迁移到核心数据时,我只有一个问题:我应该删除我的Task类并将其重新创建为Core Data中的实体?如果是这样,是否有可能将我用Core Data制作的对象添加到taskArray中,还是必须完全重新编码我的整个应用程序和cellForRowAtIndexPath?如何从实例到核心数据?

下面是我的一些代码:

Tasks.h

@interface Tasks : NSObject <NSCoding>{ 
    NSDateComponents *conversionInfo; 
} 
@property (strong, nonatomic) NSString *taskName; 
@property NSTimeInterval timeInterval; 
@property NSDate *dateCreated; 
@property (nonatomic) NSTimeInterval timeIntervalInMinutes; 
@property (nonatomic) NSTimeInterval timeIntervalInHours; 
@property (nonatomic) NSString *timeIntervalString; 
@end 

Tasks.m

@implementation Tasks 
@synthesize taskName, timeInterval, dateCreated; 
-(id) init{ 
    if (self) 
    { 
    self.taskName = taskName; 
    self.timeInterval = timeInterval; 
     self.dateCreated = dateCreated; 
    } 
    return self; 
} 
-(NSString *)timeIntervalString{ 
    NSCalendar *sysCalendar = [NSCalendar currentCalendar]; 
    NSDate *date = [NSDate date]; 
    NSDate *date1 = [NSDate dateWithTimeInterval:timeInterval sinceDate:date]; 
    unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit; 
    conversionInfo = [sysCalendar components:unitFlags fromDate:date toDate:date1 options:0]; 
    if ([conversionInfo hour] == 0){ 
     if ([conversionInfo minute] == 1) { 
      _timeIntervalString = [NSString stringWithFormat:@"%d MIN", [conversionInfo minute]]; 
     } else { 
      _timeIntervalString = [NSString stringWithFormat:@"%d MINS", [conversionInfo minute]]; 
     } 
    } else if ([conversionInfo hour] == 1) { 
     if ([conversionInfo minute] == 0){ 
     _timeIntervalString = [NSString stringWithFormat:@"%d HR", [conversionInfo hour]]; 
    } else if ([conversionInfo minute] == 1) { 
    _timeIntervalString = [NSString stringWithFormat:@"%d HR %d MIN", [conversionInfo hour], [conversionInfo minute]]; 
    } else { 
    _timeIntervalString = [NSString stringWithFormat:@"%d HR %d MINS", [conversionInfo hour], [conversionInfo minute]]; 
    } 
    } else { 
     if ([conversionInfo minute] == 0) { 
     _timeIntervalString = [NSString stringWithFormat:@"%d HRS ", [conversionInfo hour]]; 
     } else if ([conversionInfo minute] == 1){ 
      _timeIntervalString = [NSString stringWithFormat:@"%d HRS %d MIN", [conversionInfo hour], [conversionInfo minute]]; 
     } else { 
     _timeIntervalString = [NSString stringWithFormat:@"%d HRS %d MINS", [conversionInfo hour], [conversionInfo minute]]; 
     } 
    } 
    return _timeIntervalString; 
} 
@end 

TableViewController.m

-(NSMutableArray *)taskArray { 
    if (!taskArray) { 
     taskArray = [NSMutableArray array]; 
    } 
    return taskArray; 
} 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
cellSubclassCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"]; 
    if (!cell) 
     cell = [[cellSubclassCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewCell"]; 

    if([indexPath section] == 0){ 
    cell.textLabel.text = [[[self.taskArray objectAtIndex:[indexPath row]] taskName] uppercaseString]; 

    cell.imageView.image = [UIImage imageNamed:@"unchecked.png"]; 
     cell.imageView.highlightedImage = [UIImage imageNamed:@"uncheckedhighlighted.png"]; 
     [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
     [cell setBackgroundColor:[UIColor colorWithRed:236.0/255 green:240.0/255 blue:241.0/255 alpha:1.0f]]; 
     cell.textLabel.textColor = baseColor; 

     NSString *detailText = [[self.taskArray objectAtIndex:[indexPath row]] timeIntervalString]; 
     cell.detailTextLabel.text = detailText; 
       [[cell detailTextLabel] setFont:[UIFont fontWithName:@"Avenir-Black" size:12]]; 
     [[cell textLabel] setFont:[UIFont fontWithName:@"AvenirNext-DemiBold" size:16]]; 
[cell.contentView setAlpha:1]; 
    } else if ([indexPath section] == 1) { 
    cell.textLabel.text = [[[self.completedArray objectAtIndex:[indexPath row]] taskName] uppercaseString]; 

    cell.imageView.image = [UIImage imageNamed:@"checked.png"]; 
     [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
     [cell setBackgroundColor:[UIColor colorWithRed:236.0/255 green:240.0/255 blue:241.0/255 alpha:1.0f]]; 
     cell.textLabel.textColor = baseColor; 
     NSString *detailText = [[self.completedArray objectAtIndex:[indexPath row]] timeIntervalString]; 
     cell.detailTextLabel.text = detailText; 
     [[cell detailTextLabel] setFont:[UIFont fontWithName:@"Avenir-Black" size:12]]; 
     [[cell textLabel] setFont:[UIFont fontWithName:@"AvenirNext-DemiBold" size:16]]; 
     [cell.contentView setAlpha:0.5]; 
    } 
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handlechecking:)]; 
    //cell.contentView 
    [cell.imageView addGestureRecognizer:tap]; 
    cell.imageView.userInteractionEnabled = YES; 
    return cell; 
    } 
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    Tasks *task = [[Tasks alloc]init]; 
    if (indexPath.section == 0){ 
    task.taskName = [[self.taskArray objectAtIndex:[indexPath row]] taskName]; 
     task.timeInterval = [[self.taskArray objectAtIndex:[indexPath row]] timeInterval]; 
    task.dateCreated = [[self.taskArray objectAtIndex:[indexPath row]] dateCreated]; 
    } else if (indexPath.section == 1){ 
     task.taskName = [[self.completedArray objectAtIndex:[indexPath row]] taskName]; 
     task.timeInterval = [[self.completedArray objectAtIndex:[indexPath row]] timeInterval]; 
     task.dateCreated = [[self.completedArray objectAtIndex:[indexPath row]] dateCreated]; 
    } 
    DetailViewController *dvc = [[DetailViewController alloc]init]; 
    [dvc setTestTask:task]; 
    [[self navigationController] pushViewController:dvc animated:YES]; 
} 

回答

1

我应该删除我的Task类并将其重新创建为Core Data中的实体吗?

这可能是简单的改变Task以便它的NSManagedObject一个子类,创建模型,并设置Task的类模型中的任务实体。您仍然需要做一些工作来将您的应用程序转换为Core Data,但是您为任务类创建的任何逻辑仍然可能有用。

0

作为新手,我还建议您查看核心数据框架,如Sensible TableView框架。在使用Core Data时节省大量的开发时间。