iphone - 如何从实例到核心数据?

标签 iphone ios objective-c uitableview core-data

我是 iOS 编程的新手,这是我制作的第一个应用程序。基本上我在制作这个应用程序时不知道核心数据所以我正常制作类(class)。我的应用程序基本上是一个待办事项列表应用程序,其中任务作为进入 taskArray(充当 TableViewController 的数据源的数组)的对象。任务由用户输入到 taskArray。从应用程序关闭时删除的对象迁移到核心数据时,我只有一个问题:我应该删除我的 Task 类并将其重新制作为核心数据中的实体吗?如果是这样,是否可以将我使用 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

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

最佳答案

Should I delete my Task class and remake it as an entity in Core Data?

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

关于iphone - 如何从实例到核心数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17647083/

相关文章:

iphone - 仅在iPhone 4上无法将名称为 'MainWindow'的NIB装入 bundle […]中

ios - 将 IBOutlet 添加到自定义类

ios - 将复杂结构保存到 firebase

ios - 如何从 CGPDFDocumentRef 获取预告片字典?

ios - 比较数组与不同对象类型的快速方法

iphone - 滚动到 UIScrollView 中的位置

iphone - Sleep 和 NSRunLoop runMode :beforeDate: 的区别

ios - GPUImage - ChromaKeyBlendFilter - sourceImage

ios - 我需要在一个单元格和另一个单元格之间留出空间

iphone - iOS 无法在设备旋转时自动调整 subview 的大小