两个单独的控制器中的两个单独的 UITableView,加载相同的数据
第一个 .h
@interface PersonnelViewController : UIViewController
<UITableViewDelegate, UITableViewDataSource>
{
NSMutableArray *personnelData;
IBOutlet UITextField *tableCellText;
IBOutlet UITableView *personTableView;
IBOutlet UINavigationItem *navItem;
}
@property (nonatomic, retain) NSMutableArray *personnelData;
-(IBAction)addRowToTableView;
-(IBAction)editTable;
-(NSString *)personDataFilePath;
-(IBAction)endText;
-(IBAction)done;
第一个 .m
@implementation PersonnelViewController
@synthesize personnelData;
-(IBAction)done{
[self dismissModalViewControllerAnimated:YES];
}
- (void)viewDidLoad {
NSArray *archivedArray = [NSKeyedUnarchiver unarchiveObjectWithFile:[self personDataFilePath]];
if (archivedArray == nil) {
personnelData = [[NSMutableArray alloc] init];
} else {
personnelData = [[NSMutableArray alloc] initWithArray:archivedArray];
}
}
- (IBAction)addRowToTableView {
[personnelData addObject:tableCellText.text];
[self personDataFilePath];
[personTableView reloadData];
}
- (IBAction)editTable {
UIBarButtonItem *leftItem;
[personTableView setEditing:!personTableView.editing animated:YES];
if (personTableView.editing) {
leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(editTable)];
} else {
leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editTable)];
}
navItem.rightBarButtonItem = leftItem;
[self personDataFilePath];
[personTableView reloadData];
}
- (IBAction)endText {
}
- (NSInteger)numberOfSectionInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [personnelData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] autorelease];
}
cell.textLabel.text = [personnelData objectAtIndex:indexPath.row];
return cell;
}
- (NSString *)personDataFilePath {
NSString *personDataFilePath;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
personDataFilePath = [[documentDirectory stringByAppendingPathComponent:@"applicationData.plist"] retain];
return personDataFilePath;
}
- (void)saveData1 {
[NSKeyedArchiver archiveRootObject:[personnelData copy] toFile:[self personDataFilePath]];
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
[personnelData removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath {
NSString *item = [[personnelData objectAtIndex:fromIndexPath.row] retain];
[personnelData removeObject:item];
[personnelData insertObject:item atIndex:toIndexPath.row];
[item release];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (void)dealloc {
[super dealloc];
}
@end
第二个 .h >
@interface ApparatusViewController : UIViewController
<UITableViewDelegate, UITableViewDataSource> {
NSMutableArray *apparatusData;
IBOutlet UITextField *tableCellText;
IBOutlet UITableView *mainTableView;
IBOutlet UINavigationItem *navItem;
}
@property (nonatomic, retain) NSMutableArray *apparatusData;
-(IBAction)addRowToTableView;
-(IBAction)editTable;
-(NSString *)apparatusDataFilePath;
-(IBAction)endText;
-(IBAction)done;
@end
第二个.m
@implementation ApparatusViewController
@synthesize apparatusData;
-(IBAction)done{
[self dismissModalViewControllerAnimated:YES];
}
- (void)viewDidLoad {
NSArray *arichvedArray = [NSKeyedUnarchiver unarchiveObjectWithFile:[self apparatusDataFilePath]];
if (arichvedArray == nil) {
apparatusData = [[NSMutableArray alloc] init];
} else {
apparatusData = [[NSMutableArray alloc] initWithArray:arichvedArray];
}
}
- (IBAction)addRowToTableView {
[apparatusData addObject:tableCellText.text];
[self apparatusDataFilePath];
[mainTableView reloadData];
}
- (IBAction)editTable {
UIBarButtonItem *leftItem;
[mainTableView setEditing:!mainTableView.editing animated:YES];
if (mainTableView.editing) {
leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(editTable)];
} else {
leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editTable)];
}
navItem.rightBarButtonItem = leftItem;
[self apparatusDataFilePath];
[mainTableView reloadData];
}
- (IBAction)endText {
}
- (NSInteger)numberOfSectionInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [apparatusData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] autorelease];
}
cell.textLabel.text = [apparatusData objectAtIndex:indexPath.row];
return cell;
}
- (NSString *)apparatusDataFilePath {
NSString *apparatusDataFilePath;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
apparatusDataFilePath = [[documentDirectory stringByAppendingPathComponent:@"applicationData.plist"] retain];
return apparatusDataFilePath;
}
- (void)saveData {
[NSKeyedArchiver archiveRootObject:[apparatusData copy] toFile:[self apparatusDataFilePath]];
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
[apparatusData removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath {
NSString *item = [[apparatusData objectAtIndex:fromIndexPath.row] retain];
[apparatusData removeObject:item];
[apparatusData insertObject:item atIndex:toIndexPath.row];
[item release];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (void)dealloc {
[super dealloc];
}
@end
数据似乎正在将相同的数据加载到每个表中。我该如何解决这个问题。
first .h
@interface PersonnelViewController : UIViewController
<UITableViewDelegate, UITableViewDataSource>
{
NSMutableArray *personnelData;
IBOutlet UITextField *tableCellText;
IBOutlet UITableView *personTableView;
IBOutlet UINavigationItem *navItem;
}
@property (nonatomic, retain) NSMutableArray *personnelData;
-(IBAction)addRowToTableView;
-(IBAction)editTable;
-(NSString *)personDataFilePath;
-(IBAction)endText;
-(IBAction)done;
first .m
@implementation PersonnelViewController
@synthesize personnelData;
-(IBAction)done{
[self dismissModalViewControllerAnimated:YES];
}
- (void)viewDidLoad {
NSArray *archivedArray = [NSKeyedUnarchiver unarchiveObjectWithFile:[self personDataFilePath]];
if (archivedArray == nil) {
personnelData = [[NSMutableArray alloc] init];
} else {
personnelData = [[NSMutableArray alloc] initWithArray:archivedArray];
}
}
- (IBAction)addRowToTableView {
[personnelData addObject:tableCellText.text];
[self personDataFilePath];
[personTableView reloadData];
}
- (IBAction)editTable {
UIBarButtonItem *leftItem;
[personTableView setEditing:!personTableView.editing animated:YES];
if (personTableView.editing) {
leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(editTable)];
} else {
leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editTable)];
}
navItem.rightBarButtonItem = leftItem;
[self personDataFilePath];
[personTableView reloadData];
}
- (IBAction)endText {
}
- (NSInteger)numberOfSectionInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [personnelData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] autorelease];
}
cell.textLabel.text = [personnelData objectAtIndex:indexPath.row];
return cell;
}
- (NSString *)personDataFilePath {
NSString *personDataFilePath;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
personDataFilePath = [[documentDirectory stringByAppendingPathComponent:@"applicationData.plist"] retain];
return personDataFilePath;
}
- (void)saveData1 {
[NSKeyedArchiver archiveRootObject:[personnelData copy] toFile:[self personDataFilePath]];
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
[personnelData removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath {
NSString *item = [[personnelData objectAtIndex:fromIndexPath.row] retain];
[personnelData removeObject:item];
[personnelData insertObject:item atIndex:toIndexPath.row];
[item release];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (void)dealloc {
[super dealloc];
}
@end
Second .h
@interface ApparatusViewController : UIViewController
<UITableViewDelegate, UITableViewDataSource> {
NSMutableArray *apparatusData;
IBOutlet UITextField *tableCellText;
IBOutlet UITableView *mainTableView;
IBOutlet UINavigationItem *navItem;
}
@property (nonatomic, retain) NSMutableArray *apparatusData;
-(IBAction)addRowToTableView;
-(IBAction)editTable;
-(NSString *)apparatusDataFilePath;
-(IBAction)endText;
-(IBAction)done;
@end
Second .m
@implementation ApparatusViewController
@synthesize apparatusData;
-(IBAction)done{
[self dismissModalViewControllerAnimated:YES];
}
- (void)viewDidLoad {
NSArray *arichvedArray = [NSKeyedUnarchiver unarchiveObjectWithFile:[self apparatusDataFilePath]];
if (arichvedArray == nil) {
apparatusData = [[NSMutableArray alloc] init];
} else {
apparatusData = [[NSMutableArray alloc] initWithArray:arichvedArray];
}
}
- (IBAction)addRowToTableView {
[apparatusData addObject:tableCellText.text];
[self apparatusDataFilePath];
[mainTableView reloadData];
}
- (IBAction)editTable {
UIBarButtonItem *leftItem;
[mainTableView setEditing:!mainTableView.editing animated:YES];
if (mainTableView.editing) {
leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(editTable)];
} else {
leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editTable)];
}
navItem.rightBarButtonItem = leftItem;
[self apparatusDataFilePath];
[mainTableView reloadData];
}
- (IBAction)endText {
}
- (NSInteger)numberOfSectionInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [apparatusData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] autorelease];
}
cell.textLabel.text = [apparatusData objectAtIndex:indexPath.row];
return cell;
}
- (NSString *)apparatusDataFilePath {
NSString *apparatusDataFilePath;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
apparatusDataFilePath = [[documentDirectory stringByAppendingPathComponent:@"applicationData.plist"] retain];
return apparatusDataFilePath;
}
- (void)saveData {
[NSKeyedArchiver archiveRootObject:[apparatusData copy] toFile:[self apparatusDataFilePath]];
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
[apparatusData removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath {
NSString *item = [[apparatusData objectAtIndex:fromIndexPath.row] retain];
[apparatusData removeObject:item];
[apparatusData insertObject:item atIndex:toIndexPath.row];
[item release];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (void)dealloc {
[super dealloc];
}
@end
The data seems to be loading the same data into each table. How do I fix this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在两个视图中加载“applicationData.plist”!
You are loading "applicationData.plist" in both views!