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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…