ios - iOS App在重新启动应用程序之前不保存TableView结果?
问题描述
当我在我的应用程序中选择特定的颜色主题时,它只会更新在myCustomTableView
该页面上选择的颜色(从),当我转到下一页时,颜色与我选择的颜色不同,它没有更新.
如果我完全关闭应用程序(从运行中删除)然后重新加载应用程序,它将以我选择的颜色出现。似乎默认值并未实时存储在应用程序中,仅在重新启动时才存储。
文件的.m
开头也使用;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
cameraFilePath = [documentsDirectory stringByAppendingPathComponent:@"camera_Data.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:cameraFilePath])
{
NSString *bundlePath=[[NSBundle mainBundle] pathForResource:@"camera_Data" ofType:@"plist"];
[fileManager copyItemAtPath:bundlePath toPath:cameraFilePath error:nil];
}
// Custom initialization.
}
return self;
}
正在调用该表viewDidLoad
-(void) viewDidLoad{
[super viewDidLoad];
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
myCustomTableView=[[UITableView alloc] initWithFrame:CGRectMake(300, 180, 236, 672) style:UITableViewStylePlain];
else {
myCustomTableView=[[UITableView alloc] initWithFrame:CGRectMake(113, 100, 196, 372) style:UITableViewStyleGrouped];
}
[myCustomTableView setDelegate:self];
[myCustomTableView setDataSource:self];
[myCustomTableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
[myCustomTableView setSeparatorColor:[UIColor whiteColor]]; //[myCustomTableView setSeparatorColor:[UIColor blackColor]];
myCustomTableView.opaque = NO;
myCustomTableView.clipsToBounds = YES;
myCustomTableView.layer.cornerRadius=8.0;
[myCustomTableView setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:myCustomTableView];
NSLog(@"theme viewWillAppear is calling");
myDelegate = (Imax_Battery_AppAppDelegate *)[[UIApplication sharedApplication] delegate];
colorArray=[[NSMutableArray alloc] initWithObjects:@"Red",@"Green",@"Blue",@"Yellow",nil];
cellImages_Battery=[[NSMutableArray alloc] initWithObjects:@"batter-red",@"battery-green",@"battery-blue",@"yellow-smbt",nil];
flagArray=[[NSMutableArray alloc] initWithObjects: @"Beanstalk", @"DJ Talk", @"View Finder",@"Mojo Art", @"Creepy Spider",@"Purple Haze", @"Tribal Fusion", @"Radio Zapote", @"Cocanoca", @"Green Hornet",@"Cactus", @"Morning Dew", @"Quite River", @"Maple", @"River Amazon", @"Planet Vulcan", @"Planet Vulcan", @"Silent Stars", @"Helix Nebula", @"Twilight", @"Golf ", @"Football ", @"Basketball",@"Bowling", @"Tennis", nil];
flagImageArray=[[NSMutableArray alloc] initWithObjects:@"sm-a1",@"sm-a2",@"sm-a3",@"sm-a4",@"sm-a5",@"sm-g1",@"sm-g2",@"sm-g3",@"sm-g4",@"sm-g5",@"sm-n1",@"sm-n2",@"sm-n3",@"sm-n4",@"sm-n5",@"sm-s1",@"sm-s2",@"sm-s3",@"sm-s4",@"sm-s5",@"sm-sp1",@"sm-sp2",@"sm-sp3",@"sm-sp4",@"sm-sp5",nil];
bigFlagImageArray=[[NSMutableArray alloc] initWithObjects:@"a1",@"a2",@"a3",@"a4",@"a5",@"g1",@"g2",@"g3",@"g4",@"g5",@"n1",@"n2",@"n3",@"n4",@"n5",@"s1",@"s2",@"s3",@"s4",@"s5",@"sp1",@"sp2",@"sp3",@"sp4",@"sp5",nil];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
cameraFilePath = [documentsDirectory stringByAppendingPathComponent:@"camera_Data.plist"];
customArray=[[NSMutableArray alloc] initWithContentsOfFile:cameraFilePath];
myCustomTableView.separatorStyle=UITableViewCellSeparatorStyleNone ;
myCustomTableView.rowHeight=48;
self.navigationController.navigationBarHidden=YES;
[myCustomTableView reloadData];
}
行选择在这里;
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (IsSelected) {
UITableViewCell *cell=[tableView cellForRowAtIndexPath:self.tickCurrentPath];
[[cell viewWithTag:tickImageViewTag] setHidden:YES];
}
IsSelected=TRUE;
NSLog(@"the section is %d snd row %d",indexPath.section,indexPath.row);
//Below if Block is written so that the last row with plus and minus bton doesnot show tick image and else it will show
if (indexPath.section==0 && indexPath.row == [customArray count]) {
UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
[[cell viewWithTag:tickImageViewTag] setHidden:YES];
}
else {
self.tickCurrentPath=indexPath;
UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
[[cell viewWithTag:tickImageViewTag] setHidden:NO];
}
UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
UIImageView *myimageView=(UIImageView *)[cell viewWithTag:leftImageSubViewTag] ;
NSLog(@"the image view is as follows %@",myimageView.image);
if (myimageView.image && ![myimageView isHidden]) {
[self.myBatteryImageView setHidden:NO];
UIImage *myScaledImage=[self scaleImage:myimageView.image toSize:CGSizeMake(87, 296)];
self.myBatteryImageView.image=myScaledImage;
NSData *myImageData=UIImagePNGRepresentation(myScaledImage);
myDelegate.myselectedImageData = [NSMutableArray arrayWithObject:myImageData];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *lastImageFilePath = [documentsDirectory stringByAppendingPathComponent:@"LastImage.plist"];
[myDelegate.myselectedImageData writeToFile:lastImageFilePath atomically:YES];
}
else if (indexPath.section == 1) {
NSLog(@"battery images are calling RGBetc %d");
[self.myBatteryImageView setHidden:NO];
NSArray *bigBatteryColorImage=[NSArray arrayWithObjects:@"battery-red_Big",@"green-battery_Big",@"blue-battery_Big",@"yellow-battery_Big",nil];
self.myBatteryImageView.image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:[bigBatteryColorImage objectAtIndex:indexPath.row] ofType:@"png"]];
UIImage *savedImage= self.myBatteryImageView.image;
NSData *myImageData=UIImagePNGRepresentation(savedImage);
myDelegate.myselectedImageData = [NSMutableArray arrayWithObject:myImageData];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *lastImageFilePath = [documentsDirectory stringByAppendingPathComponent:@"LastImage.plist"];
[myDelegate.myselectedImageData writeToFile:lastImageFilePath atomically:YES];}
else if(indexPath.section == 2){
[self.myBatteryImageView setHidden:NO];
self.myBatteryImageView.image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:[bigFlagImageArray objectAtIndex:indexPath.row] ofType:@"png"]];
UIImage *savedImage= self.myBatteryImageView.image;
NSData *myImageData=UIImagePNGRepresentation(savedImage);
myDelegate.myselectedImageData = [NSMutableArray arrayWithObject:myImageData];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *lastImageFilePath = [documentsDirectory stringByAppendingPathComponent:@"LastImage.plist"];
[myDelegate.myselectedImageData writeToFile:lastImageFilePath atomically:YES];
}else if(indexPath.section == 3){
[self.myBatteryImageView setHidden:NO];
self.myBatteryImageView.image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:[bigFlagImageArray objectAtIndex:indexPath.row+5] ofType:@"png"]];
UIImage *savedImage= self.myBatteryImageView.image;
NSData *myImageData=UIImagePNGRepresentation(savedImage);
myDelegate.myselectedImageData = [NSMutableArray arrayWithObject:myImageData];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *lastImageFilePath = [documentsDirectory stringByAppendingPathComponent:@"LastImage.plist"];
[myDelegate.myselectedImageData writeToFile:lastImageFilePath atomically:YES];
}else if(indexPath.section == 4){
[self.myBatteryImageView setHidden:NO];
self.myBatteryImageView.image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:[bigFlagImageArray objectAtIndex:indexPath.row+10] ofType:@"png"]];
UIImage *savedImage= self.myBatteryImageView.image;
NSData *myImageData=UIImagePNGRepresentation(savedImage);
myDelegate.myselectedImageData = [NSMutableArray arrayWithObject:myImageData];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *lastImageFilePath = [documentsDirectory stringByAppendingPathComponent:@"LastImage.plist"];
[myDelegate.myselectedImageData writeToFile:lastImageFilePath atomically:YES];
}else if(indexPath.section == 5){
[self.myBatteryImageView setHidden:NO];
self.myBatteryImageView.image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:[bigFlagImageArray objectAtIndex:indexPath.row+15] ofType:@"png"]];
UIImage *savedImage= self.myBatteryImageView.image;
NSData *myImageData=UIImagePNGRepresentation(savedImage);
myDelegate.myselectedImageData = [NSMutableArray arrayWithObject:myImageData];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *lastImageFilePath = [documentsDirectory stringByAppendingPathComponent:@"LastImage.plist"];
[myDelegate.myselectedImageData writeToFile:lastImageFilePath atomically:YES];
}else if(indexPath.section == 6){
[self.myBatteryImageView setHidden:NO];
self.myBatteryImageView.image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:[bigFlagImageArray objectAtIndex:indexPath.row+20] ofType:@"png"]];
UIImage *savedImage= self.myBatteryImageView.image;
NSData *myImageData=UIImagePNGRepresentation(savedImage);
myDelegate.myselectedImageData = [NSMutableArray arrayWithObject:myImageData];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *lastImageFilePath = [documentsDirectory stringByAppendingPathComponent:@"LastImage.plist"];
[myDelegate.myselectedImageData writeToFile:lastImageFilePath atomically:YES];
}
else {
if ([myDelegate.myselectedImageData count]>0) {
[myDelegate.myselectedImageData removeAllObjects];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *lastImageFilePath = [documentsDirectory stringByAppendingPathComponent:@"LastImage.plist"];
[myDelegate.myselectedImageData writeToFile:lastImageFilePath atomically:YES];
}
[self.myBatteryImageView setHidden:YES];
}
}
解决方案
推荐阅读
- mysql - Typeorm - 无法更新实体,因为实体中未设置实体 ID
- java - Java同步奇怪的东西
- javascript - Stripe.js 和 hcaptcha,其中 hcaptcha 绑定到按钮
- docker - `docker build` 和 `docker run` 的 IP 地址相同
- iframe - iframe 内容未在无头 chrome 中加载
- python - 添加到 django 管理面板的链接
- javascript - 事件期间的状态一致性和时间安排
- python - 在模型继承中使用 selectin 加载
- pandas - 尝试在 DataFrame 中使用 linspace 但我无法以正确的格式获得输出
- mysql - SQL以不同格式显示当前时间戳Mysql