gpt4 book ai didi

ios - 使用 nsuserdefault 在 uitableviewcell 中保存图像状态

转载 作者:行者123 更新时间:2023-11-29 02:20:53 25 4
gpt4 key购买 nike

我已经为这个问题苦苦挣扎了几个星期,我不好意思说,并且搜索了整个堆栈溢出和互联网,但找不到任何适合我的东西。我遇到过一些非常模糊的答案,它们说,“好吧,使用这两行代码,然后添加这两行,然后可能将其放入属性中……”到底是什么?没有人说过将代码放在哪里,也很少有人指出需要在 cellForRowAtIndexpath 中完成什么。我已经尝试了很多东西!

我只是想在我的表格 View 单元格中保存 UIImage 的状态。基本上,用户可以点击 tableview 单元格中的图像,当他们这样做时,会弹出一个 UIAlertview。他们可以选择是否使用了表格 View 中列出的交通工具类型。如果"is",图像颜色会变为蓝色(而不是灰色),表明他们已经使用过它。或者,他们可以通过点击“否”来取消选择图像,图像将返回其灰色状态。我希望能够使用 nsuserdefaults 保留此操作,因为只有少量信息要保存(基本上只是图像状态)。我试过的任何东西都不会保存信息。我将在下面添加我的代码,并祝愿任何能帮助我的人幸福快乐!

或者,我从自定义类中创建了运输对象。有没有办法在自定义类中设置一个 bool 值,当用户选择是或否时动态更改它,然后更新自定义类以保存信息?任何想法,将不胜感激!

主视图 Controller

@interface MainTableViewController ()
{
NSMutableArray *selectedImages;
}


@end

@implementation MainTableViewController

-(void)viewDidLoad
{
[super viewDidLoad];

_transportController = [[TransportDataController alloc] init];
self.dataSource = _transportController.populateDataSource;
self.title = @"Transportation Types";

selectedImages = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"selected"] mutableCopy];


}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"mainCell";
TransportCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
cell = [[TransportCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

Transport *transportData = [self.dataSource objectAtIndex:indexPath.row];
cell.nameLabel.text = transportData.name;
cell.transportImageView.image = transportData.transportImage;


if (transportData.usedTransportIsSelected == NO)
{
cell.grayedImageView.image = transportData.usedTransportImage;


}
else if (transportData.usedTransportIsSelected == YES)
{
cell.grayedImageView.image = [UIImage imageNamed:@"stamp-color"];


}


UITapGestureRecognizer *grayedImageTouched = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(transportImageTapped:)];
grayedImageTouched.numberOfTapsRequired = 1;
cell.grayedImageView.tag = indexPath.row;
[cell.grayedImageView addGestureRecognizer:grayedImageTouched];
cell.grayedImageView.userInteractionEnabled = YES;



return cell;
}


-(void)transportImageTapped:(UIGestureRecognizer *)gesture
{
Transport *transportData = [self.dataSource objectAtIndex:gesture.view.tag];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:gesture.view.tag inSection:0];


UIAlertController *transportAlert = [UIAlertController alertControllerWithTitle:@"Yes, it's true..." message:@"I have used this type of transport before." preferredStyle:UIAlertControllerStyleAlert];

[transportAlert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){

NSLog(@"cancel");

}]];

[transportAlert addAction:[UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){

UIImage *darkImage = [UIImage imageNamed:@"stamp-color"];
transportData.usedTransportImage = darkImage;
transportData.usedTransportIsSelected = YES;
[self.dataSource replaceObjectAtIndex:gesture.view.tag withObject:transportData];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:@"selected"];
[defaults synchronize];

NSLog(@"has taken this transport before");

}]];

[transportAlert addAction:[UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){

UIImage *grayedImage = [UIImage imageNamed:@"stamp-grayed"];
transportData.usedTransportImage = grayedImage;
transportData.usedTransportIsSelected = NO;
[self.dataSource replaceObjectAtIndex:gesture.view.tag withObject:transportData];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:NO forKey:@"selected"];
[defaults synchronize];

NSLog(@"has not taken this transport before");

}]];

[self presentViewController:transportAlert animated:YES completion:nil];

}

最佳答案

修改 Transport 类对您来说可能更有意义;假设它附加了一些唯一标识符。

@interface Transport
@property (strong) id somethingThatUniquelyIdentifiesThisTransport;
@property (nonatomic) BOOL usedTransportIsSelected;
// ...
@end

@implementation Transport
- (void)setUsedTransportIsSelected:(BOOL)value {
[[NSUserDefaults standardUserDefaults] setObject:@(value) forKey:self.somethingThatUniquelyIdentifiesThisTransport];
}

- (BOOL)usedTransportIsSelected {
return [[[NSUserDefaults standardUserDefaults] objectForKey:self.somethingThatUniquelyIdentifiesThisTransport] boolValue];
}

// ...
@end

关于ios - 使用 nsuserdefault 在 uitableviewcell 中保存图像状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28154055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com