【发布时间】:2016-04-29 17:39:27
【问题描述】:
我正在尝试通过加载与他们在传递中单击的单元格相对应的数字列表来预选表格视图中的单元格。我从 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
问题是,当我返回导航控制器并再次前进到 tableview 时,未选择行,但如果我在导航控制器中前进并返回,默认情况下会选择它们(我不不必为此做任何事情)。
更新:
我检查并排除了一些不相关的通用方法。
【问题讨论】:
-
你能把断点放在 data = [[[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"DictKey"] allKeys] mutableCopy];线。当您返回导航控制器并再次前进到表格视图时。并更新推送 Table VC 的代码。
标签: ios objective-c uitableview