在我的应用程序中,一个是mainviewcontroller,它是uiTableviewcontroller的子类,当点击一个特定的单元格时,它会从plist中保存数据,它显示的是detail视图控制器(dvc),它是uiviewcontroller的子类。 dvc持有textfield&按钮。实际上,当我点击按钮时,我通过plist在tableviewcell上写文本字段的文本,但它实际上是暂时写入数据但我想在tableview上永久写入数据我能做些什么。基于导航的应用程序?
答案 0 :(得分:0)
操作按钮的代码是什么?你如何重新加载表视图?
我猜你正在读一本关于NSDictionary的plist?然后从字典中的值加载tableviews cell.text?当您按下按钮时,您是否将文本字段文本写入NSDIctionary?你还记得把NSDictionary写回plist吗?
答案 1 :(得分:0)
好。 你如何加载你的tableview单元格? 你确定col1的值是NSString类型的吗? 您应该使用setObject:forKey:而不是SetValue:forKey:.. tableview根本没有更新,还是只是在你退出后没有保存? 你在模拟器或设备上测试吗? 应用程序包是设备上的只读目录,但不在模拟器中。你应该使用:
NSString * rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, 是)objectAtIndex:0];
NSString * plistPath = [rootPath stringByAppendingPathComponent:@ “listData.plist”];
答案 2 :(得分:0)
试试这个:
将plist加载到字典:
//Get the path to documents directory
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [rootPath stringByAppendingPathComponent:@"listData.plist"];
//if the file does not exist in documents directory, then load from the bundle.
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
plistPath = [[NSBundle mainBundle] pathForResource:@"listData" ofType:@"plist"];
}
//load plist into NSMutableDictionary.
nes.plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
将词典保存为plist:
//Get the path to documents directory
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [rootPath stringByAppendingPathComponent:@"listData.plist"];
//Write to documents directory! NOT bundle!!
[nes.plistDict writeToFile:plistPath atomically: YES];
还使用: [nes.plistDict setObject:txtName.text forKey:@“col1”]; 不: [nes.plistDict setValue:txtName.text forKey:@“col1”];
如果这没有用,我可以在mainviewcontroller中看到你的cellForRowAtIndexPath吗?