사용자가 셀을 터치하여 작업을 확인/선택 취소 할 수있는 간단한 할일 앱이 있습니다. 하지만 코어 데이터를 부울 값을 yes로 설정하여 업데이트하는 방법을 잘 모르겠습니다.UITableViewCell touch에서 핵심 데이터 개체 업데이트
여기 내 코드입니다 :
// PakkelisteViewController.m // Sommerleir// // // 08.05.14에 로니 - 앙드레 Bendiksen 작성. // Copyright (c) 2014 Arbeidernes Ungdomsfylking. 판권 소유. //
#import "PakkelisteViewController.h"
#import "AUFToDoItem.h"
#import "TodoCell.h"
@interface PakkelisteViewController()
@property NSMutableArray *toDoItems;
-(IBAction)addNewToDoItem;
@end
@implementation PakkelisteViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.toDoItems = [[NSMutableArray alloc] init];
[self loadInitialData];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
-(void)viewWillAppear:(BOOL)animated
{
// Fetch the devices from persistent data store
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"AUFToDoItem"];
self.toDoItems = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
[self.tableView reloadData];
}
-(void)loadInitialData
{
// check if user has already been using this functionality
BOOL hasRunBefore = [[NSUserDefaults standardUserDefaults] boolForKey:@"PakkelisteHasRun"];
if (!hasRunBefore)
{
NSLog(@"Has not run");
[self.toDoItems addObjectsFromArray:@[@"Badetøy", @"Skrivesaker", @"Lommepenger", @"Godt humør"]];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"PakkelisteHasRun"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
else
{
NSLog(@"Already run");
}
}
-(IBAction)addNewToDoItem
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Legg til ny" message:nil delegate:self cancelButtonTitle:@"Avbryt" otherButtonTitles:@"Legg til", nil];
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
[[alertView textFieldAtIndex:0] setPlaceholder:@"Rent undertøy"];
[[alertView textFieldAtIndex:0] setAutocapitalizationType:UITextAutocapitalizationTypeSentences];
[alertView show];
}
-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
NSString *inputText = [[alertView textFieldAtIndex:0] text];
if ([inputText length] > 0)
{
return YES;
}
else
{
return NO;
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1)
{
NSManagedObjectContext *context = [self managedObjectContext];
// Create a new managed object
NSManagedObject *toDoItem = [NSEntityDescription insertNewObjectForEntityForName:@"AUFToDoItem" inManagedObjectContext:context];
[toDoItem setValue:[alertView textFieldAtIndex:0].text forKey:@"itemName"];
[toDoItem setValue:[NSDate date] forKey:@"creationDate"];
[toDoItem setValue:[NSNumber numberWithBool:YES] forKey:@"completed"];
[self.toDoItems addObject:toDoItem];
NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
[self dismissViewControllerAnimated:YES completion:nil];
[self.tableView reloadData];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.toDoItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
TodoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[TodoCell alloc] init];
}
NSManagedObject *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
cell.title.text = [toDoItem valueForKey:@"itemName"];
if ([[toDoItem valueForKey:@"completed"] boolValue] == 1)
{
cell.checkbox.image = [UIImage imageNamed:@"checkbox_on.png"];
}
else
{
cell.checkbox.image = [UIImage imageNamed:@"checkbox_on.png"];
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
/*
[tableView deselectRowAtIndexPath:indexPath animated:NO];
AUFToDoItem *tappedItem = [self.toDoItems objectAtIndex:indexPath.row];
tappedItem.completed = !tappedItem.completed;
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
*/
}
-(NSManagedObjectContext *)managedObjectContext {
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObjectContext *context = [self managedObjectContext];
if (editingStyle == UITableViewCellEditingStyleDelete) {
[context deleteObject:[self.toDoItems objectAtIndex:indexPath.row]];
NSError *error = nil;
if (![context save:&error])
{
NSLog(@"Can't delete! %@ %@", error, [error localizedDescription]);
return;
}
[self.toDoItems removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
코어 데이터 문서에서 쉽게 찾아 볼 수있는 것처럼 지금까지 시도한 것을 말해 줄 수 있습니다. 코드의 기본 논리는 셀 선택 처리, 업데이트 할 객체 가져 오기, 관련 속성 업데이트, 컨텍스트 저장과 같습니다. – Calvedos
NSFetchedResultsController 구현은 좋은 옵션이 될 것이며 섹션 및 행 삽입, 업데이트 및 삭제를 처리합니다. – Jonathan
@Jonathan Apple 설명서가 다소 혼란 스럽습니다. 이 튜토리얼이나 비디오를 잘 아십니까? –