本文介绍了保存 UITableView 数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用故事板和动态 UITableView 添加我的单元格,如下所示.我需要知道如何将此文本保存在每个单元格的 UITextField 以及 UISwitch 状态等中,因此当我上下滚动时,数据不会丢失.此外,当应用程序关闭时,我需要它显示重新打开时输入的数据.我对编程很陌生,这是我的第一个 UItableView 项目.编码示例会很棒,请提供尽可能详细的信息!提前致谢!

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];

推荐答案

我认为你可以制作一个模型对象来存储数据.

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

.h 文件

@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 文件

#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 数据源

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

这篇关于保存 UITableView 数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 13:56