gpt4 book ai didi

ios - UITableview 不通过 willdisplaycell 选择单元格

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

我试图通过加载与他们在传递中单击的单元格相对应的数字列表来预选择表格 View 中的单元格。我从 nsuserdefaults 加载它并使用 willdisplaycell 方法单击单元格。我已经包含了我用来执行此操作的 tableviewcontroller 类。

@implementation AllergenTableViewController

static NSMutableArray *data;

-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];

if (self)
{
data = [[[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"DictKey"] allKeys] mutableCopy];
self.allergens = [[NSMutableArray alloc] init];
}

return self;
}

- (void)viewDidLoad {
[super viewDidLoad];

// Uncomment the following line to preserve selection between presentations.
self.clearsSelectionOnViewWillAppear = NO;

//first need to load in array to a number list, then convert it to index path list and set allergens equal to that
NSMutableArray *allergenNumberList;

allergenNumberList = [[[NSUserDefaults standardUserDefaults] objectForKey:@"allergens"] mutableCopy];

if (allergenNumberList != nil)
{
for (NSNumber *num in allergenNumberList)
{
NSIndexPath *path = [NSIndexPath indexPathForRow:num.integerValue inSection:1]; //[NSIndexPath indexPathWithIndex:num.integerValue];

if (![self.allergens containsObject:path])
{
[self.allergens addObject:path];
}
}
}

NSLog(@"loading view, allergens size: %lu", [self.allergens count]);
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self.allergens containsObject:indexPath])
{
[cell setSelected:YES animated:NO];
}
}

....

// the cell will be returned to the tableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"allergenslabel"forIndexPath:indexPath];

if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"allergenslabel"];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}

cell.textLabel.text = [NSString stringWithFormat:@"%@", data[indexPath.row]];

return cell;
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSMutableArray *arr = [[NSMutableArray alloc] init];

for (NSIndexPath *idx in self.allergens)
{
[arr addObject:[NSNumber numberWithInteger:idx]];
}

[[NSUserDefaults standardUserDefaults] setObject:arr forKey:@"allergens"];
NSLog(@"saving");
NSLog(@"allergens count: %lu", [self.allergens count]);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

//contains isnt working because the index pths arent the same but have the same row numbers so check those instead
if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
{
cell.accessoryType = UITableViewCellAccessoryNone;

if ([self.allergens containsObject:indexPath])
{
[self.allergens removeObject:indexPath];
}
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;

if (![self.allergens containsObject:indexPath])
{
[self.allergens addObject:indexPath];
NSLog(@"adding object");
}
else
{
NSLog(@"object already in allergens list");
}
}
}

@end

问题是,当我返回导航 Controller 并再次前进到表格 View 时,行不会被选中,但是如果我在导航 Controller 中前进并返回,它们将被默认选中(我不无需做任何事情即可实现这一点)。

更新:

我检查并排除了一些并不真正相关的通用方法。

最佳答案

当您返回导航 Controller 时, View Controller 被释放,因此选择行的数据丢失。向前导航到此页面将实例化 View Controller 的新实例。那不再是您之前看到的同一张 table 。当您在导航中前进(在堆栈上抛出一个新的 View Controller )时,带有表格的 View Controller 保留在导航堆栈中,因此数据仍然存在,因为您离开了它。您将需要找到其他地方来保存数据并将其传递给这个新实例。

关于ios - UITableview 不通过 willdisplaycell 选择单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36944438/

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