开发者

Save Text from UITableViews

EDIT: Found the actual cause of the crashing. I needed to delete and reinstall to test properly without crashing as I altered the data model. However its now a case of working out how to correctly save an array with coredata. How can I do this?

I have a UITableView and I want to be able to enter text into the cells then save that text in an array, then load the text back in from the array when you load the app.

Im using CoreData but seem to be having some issues. Here is what I have thus far.

In the cellForRowAtIndex function I am adding UITextFields as a subview of each cell so I can type in them and I can only type in them when my UISwitch, editCells, is on. Four of the cells have preset text in them which I have set.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(15, 20, 185, 30)];
    textField.delegate = self;

    // Configure the cell...

    if ([indexPath row] == 0) {
        [cell setAccessoryView:textField];
 开发者_如何学编程       textField.text = @"Before School";
        textField.font = [UIFont fontWithName:@"Helvetica-Oblique" size:16];
        textField.enabled = FALSE;
    }

    else if ([indexPath row] == 3) {
        [cell setAccessoryView:textField];
        textField.text = @"Break Time";
        textField.font = [UIFont fontWithName:@"Helvetica-Oblique" size:16];
        textField.enabled = FALSE;
    }

    else if ([indexPath row] == 6) {
        [cell setAccessoryView:textField];
        textField.text = @"Lunchtime";
        textField.font = [UIFont fontWithName:@"Helvetica-Oblique" size:16];
        textField.enabled = FALSE;
    }

    else if ([indexPath row] == 9) {
        [cell setAccessoryView:textField];
        textField.text = @"After School";
        textField.font = [UIFont fontWithName:@"Helvetica-Oblique" size:16];
        textField.enabled = FALSE;
    }

    else if ([indexPath row] == 1 || [indexPath row] == 2 || [indexPath row] == 4 || [indexPath row] == 5 || [indexPath row] == 7 || [indexPath row] == 8) {
        [cell setAccessoryView:textField];
        textField.font = [UIFont fontWithName:@"Helvetica" size:16];
        if (editCells.on == TRUE) {
            textField.enabled = TRUE;
        }
        else {
            textField.enabled = FALSE;
        }
    }

    [monArrayA addObject:textField.text];

    [textField release];

    return cell;
}

To save I run this when the value of editCells changes, so when the user has finished editing its saved as this switched will be turned off. I make a new object to save with core data. I save the lesson which is the array with all the cell text in and the table it needs to be in, but saving its tag. But of course, this chucks up a SIGABRT error and crashes the app when flicking the switch.

[editCells addTarget:self action:@selector(editCells:) forControlEvents:UIControlEventValueChanged];

-(void)editCells:(id)sender{
    [monTable reloadData];

    //Create a new object to save.
    NSManagedObject *newLesson;
    //Tell it to be saved to the DatedText entity using the managedObjectContext, context_.
    newLesson = [NSEntityDescription insertNewObjectForEntityForName:@"TimeTable" inManagedObjectContext:self.managedObjectContext];
    //Set two values within this object, being the text the user entered and the date from the datePicker.
    //Save these to their appropriate properties within this entity.
    [newLesson setValue:monArrayA forKey:@"lessonName"];
    [newLesson setValue:monTable.tag forKey:@"table"];

    //Create error incase of failure to save.
    NSError *saveError = nil;
    //Try and access the data store via context_ and fall back on saveError if fails.
    [self.managedObjectContext save:&saveError];
    if (saveError != nil) {
        //Report error by printing it to the console.
        NSLog(@"[%@ saveContext] Error saving context: Error = %@, details = %@",[self class], saveError,saveError.userInfo);
    }
}


Tableviews work a little differently than you're expecting. The important thing to remember is that tableView:cellForRowAtIndexPath: is called both to create a cell, and to reuse one. You should only create subviews (like your textField) in the if (cell == nil) path. Make sure to give each field a unique tag and add them as subviews of the cell.

Then, in the configure cell path (after potentially creating the elements) you get the fields again by calling [cell.contentView viewWithTag:YOUR_FIELD_TAG] and then update the field.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜