保存表格视图单元格的数据

时间:2015-02-20 07:08:24

标签: ios objective-c uitableview

我在视图控制器中有3个文本字段,一个提交按钮和一个表视图。现在我想在表格视图中将3个文本字段的数据显示为自定义单元格的3个标签,当我再次更新数据时,更新的数据应显示在下一个单元格中。但在我的代码中,更新的数据显示在单元格中。以前的数据是覆盖的。任何人都可以告诉我哪里错了?

这是我的ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *addressTextField;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
- (IBAction)submitButton:(id)sender;
@property (weak, nonatomic) IBOutlet UITableView *detailTable;

@property(nonatomic, strong)NSMutableArray *arrPeopleDetail;


@end

这是我的ViewController.m

#import "ViewController.h"
#import "detailObject.h"

@interface ViewController ()

@end


@implementation ViewController
{
detailObject *peopleDetail;
}

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.
self.arrPeopleDetail = [[NSMutableArray alloc]init];
peopleDetail = [[detailObject alloc]init];

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)submitButton:(id)sender {

[self.detailTable reloadData];


if ([self.nameTextField.text isEqualToString:@""] || [self.addressTextField.text isEqualToString:@""] || [self.phoneTextField.text isEqualToString:@""] ) {
    UIAlertView *alrt = [[UIAlertView alloc]initWithTitle:@"Warning" message:@"Plaeas Enter Text In The Empty Field" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil ];
    [alrt show];
}
else{

    peopleDetail.strPeopleName = self.nameTextField.text;
    peopleDetail.strPeopleAddress = self.addressTextField.text;
    peopleDetail.strPeoplePhoneNumber = self.phoneTextField.text;

    [self.arrPeopleDetail addObject:peopleDetail];


}

//[self.detailTable reloadData];


}


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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];

UILabel *lbl1 = (UILabel*)[cell.contentView viewWithTag:1];
lbl1.text = [[self.arrPeopleDetail objectAtIndex:indexPath.row]valueForKey:@"strPeopleName"];

UILabel *lbl2 = (UILabel*)[cell.contentView viewWithTag:2];
lbl2.text = [[self.arrPeopleDetail        objectAtIndex:indexPath.row]valueForKey:@"strPeopleAddress"];

UILabel *lbl3 = (UILabel*)[cell.contentView viewWithTag:3];
lbl3.text = [[self.arrPeopleDetail objectAtIndex:indexPath.row]valueForKey:@"strPeoplePhoneNumber"];

return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return [self.arrPeopleDetail count];


}


@end

这是我拍摄的.h NSObject类。

#import <Foundation/Foundation.h>

@interface detailObject : NSObject

@property(nonatomic,strong) NSString *strPeopleName;
@property(nonatomic,strong) NSString *strPeopleAddress;
@property(nonatomic,strong) NSString *strPeoplePhoneNumber;

@end

2 个答案:

答案 0 :(得分:0)

添加行peopleDetail = [[detailObject alloc] init];在其他情况下,submitButton操作并将新对象添加到数组重装表视图数据

之后

否则{

peopleDetail = [[detailObject alloc]init];
peopleDetail.strPeopleName = self.nameTextField.text;
peopleDetail.strPeopleAddress = self.addressTextField.text;
peopleDetail.strPeoplePhoneNumber = self.phoneTextField.text;

[self.arrPeopleDetail addObject:peopleDetail];
[self.detailTable reloadData];

}

你可以删除peopleDetail = [[detailObject alloc] init];来自viewDidLoad

答案 1 :(得分:0)

我意识到我错了。我在全局创建对象而不是在本地创建它。如果有人犯了这个错误,那么每当用户输入数据时数据被覆盖,因为创建的对象是一个。所以主要的错误是全局创建对象而不是在本地创建它。

因此更新的ViewController.m是..

#import "ViewController.h"
#import "detailObject.h"

@interface ViewController ()

@end


@implementation ViewController


- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.
self.arrPeopleDetail = [[NSMutableArray alloc]init];

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)submitButton:(id)sender {

if ([self.phoneTextField.text isEqualToString:@""] && [self.addressTextField.text isEqualToString:@""] && [self.phoneTextField.text isEqualToString:@""] ) {
    UIAlertView *alrt = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Please Fillup The Above Fields" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
    [alrt show];
}

else if ([self.nameTextField.text isEqualToString:@""]) {
    UIAlertView *alrt_name = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Plaeas Enter Name" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil ];
    [alrt_name show];
}

else if ([self.addressTextField.text isEqualToString:@""]) {
        UIAlertView *alrt_address = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Please Enter Address" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
        [alrt_address show];
}
else if ([self.phoneTextField.text isEqualToString:@""]) {
        UIAlertView *alrt_phoneNumber = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Please Enter Phone Number" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
        [alrt_phoneNumber show];
}

else
{
    detailObject *peopleDetail = [[detailObject alloc] init];
    peopleDetail.strPeopleName = self.nameTextField.text;
    peopleDetail.strPeopleAddress = self.addressTextField.text;
    peopleDetail.strPeoplePhoneNumber = self.phoneTextField.text;

    [self.arrPeopleDetail addObject:peopleDetail];

    self.nameTextField.text = @"";
    self.addressTextField.text = @"";
    self.phoneTextField.text = @"";

}

[self.detailTable reloadData];


}


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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];


UILabel *lbl1 = (UILabel*)[cell.contentView viewWithTag:1];
lbl1.text = [[self.arrPeopleDetail objectAtIndex:indexPath.row]valueForKey:@"strPeopleName"];

UILabel *lbl2 = (UILabel*)[cell.contentView viewWithTag:2];
lbl2.text = [[self.arrPeopleDetail objectAtIndex:indexPath.row]valueForKey:@"strPeopleAddress"];

UILabel *lbl3 = (UILabel*)[cell.contentView viewWithTag:3];
lbl3.text = [[self.arrPeopleDetail objectAtIndex:indexPath.row]valueForKey:@"strPeoplePhoneNumber"];

return cell;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return [self.arrPeopleDetail count];

}


@end