Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

ios - Save UITableView data?

I am using storyboards and a dynamic UITableView adding my cells like below. I need to know how I can save this text in the UITextField in each cell as well as UISwitch state etc so when I scroll up and down, the data is not lost. Also when the app closes, I need it to show the data entered when reopening. I am pretty new to programming, and this is my first UItableView project. Coding examples would be great, please give as much detail as possible! thank you in advance!

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

    // Make cell unselectable and set font.
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = [UIFont fontWithName:@"ArialMT" size:13];

if (indexPath.section == 0) {

    UITextField* tf = nil;
    switch ( indexPath.row ) {
        case 0: {
            cell.textLabel.text = @"Name" ;
            tf = nameFieldTextField = [self makeTextField:self.name placeholder:@"John Appleseed"];
            nameFieldTextField.tag = 1;
            [cell addSubview:nameFieldTextField];
            break ;
        }
        case 1: {
            cell.textLabel.text = @"Address" ;
            tf = addressFieldTextField = [self makeTextField:self.address placeholder:@"Street Address"];
            addressFieldTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
            [cell addSubview:addressFieldTextField];
            break ;
        }
        case 2: {
            cell.textLabel.text = @"Email" ;
            tf = emailFieldTextField = [self makeTextField:self.email placeholder:@"[email protected]"];
            emailFieldTextField.keyboardType = UIKeyboardTypeEmailAddress;
            [cell addSubview:emailFieldTextField];
            break ;
        }
        case 3: {
            cell.textLabel.text = @"Phone" ;
            tf = phoneFieldTextField = [self makeTextField:self.phone placeholder:@"xxx-xxx-xxxx"];
            phoneFieldTextField.keyboardType = UIKeyboardTypePhonePad;
            phoneFieldTextField.tag = 10;
            phoneFieldTextField.text = [self formatPhoneNumber:phoneFieldTextField.text deleteLastChar:YES];
            [cell addSubview:phoneFieldTextField];
            break ;
        }
        case 4: {
            cell.textLabel.text = @"DOB" ;
            tf = dateOfBirthTextField = [self makeTextField:self.dateOfBirth placeholder:@"xx/xx/xxxx"];
            dateOfBirthTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
            [cell addSubview:dateOfBirthTextField];
            break ;
        }

    }
    // Textfield dimensions
    tf.frame = CGRectMake(120, 12, 170, 30);

    // Workaround to dismiss keyboard when Done/Return is tapped
    [tf addTarget:self action:@selector(textFieldFinished:) forControlEvents:UIControlEventEditingDidEndOnExit];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you can make a model object for storing the data.

.h file

@interface User : NSObject <NSCoding>

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *address;
@property (nonatomic, copy) NSString *email;
@property (nonatomic, copy) NSString *phone;
@property (nonatomic, copy) NSString *dob;

- (void)save;
+(User *)savedUser; 

.m file

#define kName    @"Name"
#define kAddress @"Address"
#define kEMail   @"EMail"
#define kPhone   @"Phone"
#define kDOB     @"DOB"

#define kSavedUser @"SavedUser"

@implementation User

- (void)encodeWithCoder:(NSCoder *)encoder{

    [encoder encodeObject:self.name     forKey:kName];
    [encoder encodeObject:self.address  forKey:kAddress];
    [encoder encodeObject:self.email    forKey:kEMail];
    [encoder encodeObject:self.phone    forKey:kPhone];
    [encoder encodeObject:self.dob      forKey:kDOB];

}

- (id)initWithCoder:(NSCoder *)decoder{
    self = [super init];
    if (self) {
        self.name       = [decoder decodeObjectForKey:kName];
        self.address    = [decoder decodeObjectForKey:kAddress];
        self.email      = [decoder decodeObjectForKey:kEMail];
        self.phone      = [decoder decodeObjectForKey:kPhone];
        self.dob        = [decoder decodeObjectForKey:kDOB];
    }

    return self;
}

#pragma mark - Save

- (void)save{

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self];
    [defaults setObject:data forKey:kSavedUser];
    [defaults synchronize];
}

+(User *)savedUser{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *data = [defaults objectForKey:kSavedUser];
    return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}

UITableView DataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier
                                                            forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.detailTextField.delegate = self;
    [cell.detailTextField setTag:indexPath.row+1];

    switch (indexPath.row+1) {
        case 1:{
            cell.titleLabel.text = @"Name";
            cell.detailTextField.text = self.user.name;
            break;
        }
        case 2:{
            cell.titleLabel.text = @"Address";
            cell.detailTextField.text = self.user.address;
            break;
        }
        case 3:{
            cell.titleLabel.text = @"EMail";
            cell.detailTextField.text = self.user.email;
            break;
        }
        case 4:{
            cell.titleLabel.text = @"Phone";
            cell.detailTextField.text = self.user.phone;
            break;
        }
        case 5:{
            cell.titleLabel.text = @"D.O.B.";
            cell.detailTextField.text = self.user.dob;
            break;
        }

        default:
            break;
    }

    return cell;
}

It's too much to explain. I have attached source code, take a look. Source Code


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...