私は iOS プログラミングは初めてで、これは私が今までに作成した最初のアプリです。基本的にこのアプリを作る時はコアデータのことを知らなかったので普通にクラスを作りました。私のアプリは基本的に、タスクを taskArray (TableViewController へのデータ ソースとして機能する配列) に入るオブジェクトとして持つ to do リスト アプリです。タスクは、ユーザーによって taskArray に入力されます。アプリがコア データに閉じたときに消去されるオブジェクトから移行するときに、1 つだけ質問があります。タスク クラスを削除して、コア データ内のエンティティとして再作成する必要がありますか? その場合、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];
}