1

我是 iOS 编程新手,这是我制作的第一个应用程序。基本上我在制作这个应用程序时不了解核心数据,所以我正常制作了课程。我的应用程序基本上是一个待办事项列表应用程序,其中任务作为进入 taskArray(充当 TableViewController 数据源的数组)的对象。任务由用户输入到 taskArray。当从应用程序关闭核心数据时被擦除的对象迁移时,我只有一个问题:我应该删除我的任务类并将其重新制作为核心数据中的实体吗?如果是这样,是否可以将我使用 Core Data 制作的对象添加到 taskArray,或者我是否必须完全改造我的整个应用程序和 cellForRowAtIndexPath?

这是我的一些代码:

任务.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

任务.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

表视图控制器.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];
}
4

2 回答 2

1

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

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

于 2013-07-15T05:15:05.907 回答
0

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

于 2013-07-15T19:40:23.470 回答