gpt4 book ai didi

iphone - View 消失后如何在 UITableView 中保留复选标记

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

我有一个 uitableview,它显示带有自定义复选标记的多个选择。选择后,使用 NSUserDefaults 保存行值。问题是尽管保存了值,复选标记从表格单元格行中消失了。我不明白为什么。

感谢您的帮助,我真的很困惑。

这是 .h 代码:

    @interface CategoriesViewController : UITableViewController {

NSString *selectedCategoryTableString;

NSString *jsonStringCategory;

int prev;

}

// arForTable array will hold the JSON results from the api

@property (nonatomic, retain) NSArray *arForTable;
@property (nonatomic, retain) NSMutableArray *arForIPs;

@property (nonatomic, retain) NSMutableArray *categorySelected;

@property (nonatomic, retain) NSString *jsonStringCategory;
@property(nonatomic, retain) UIView *accessoryView;

@end

和 .m 代码:​​

@implementation CategoriesViewController
@synthesize jsonStringCategory;
@synthesize arForTable = _arForTable;
@synthesize arForIPs = _arForIPs;

- (void)viewDidLoad
{
[super viewDidLoad];

self.arForIPs=[NSMutableArray array];

self.categorySelected = [[NSMutableArray alloc] init];

[self reloadMain];

self.tableView.allowsMultipleSelection = YES;
}

-(void) reloadMain {

jsonString = @"http:///******";

// Download the JSON
NSString *jsonString = [NSString
stringWithContentsOfURL:[NSURL URLWithString:jsonString]
encoding:NSStringEncodingConversionAllowLossy|NSUTF8StringEncoding
error:nil];

NSMutableArray *itemsTMP = [[NSMutableArray alloc] init];

// Create parser
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *results = [parser objectWithString:jsonString error:nil];

itemsTMP = [results objectForKey:@"results"];

self.arForTable = [itemsTMP copy];

[self.tableView reloadData];

}

#pragma mark - Table view data source

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

- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.arForTable count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

[cell.textLabel setFont:[UIFont fontWithName: @"Asap-Bold" size: 14.0f]];
[cell.detailTextLabel setFont:[UIFont fontWithName: @"Asap-Bold" size: 14.0f]];
cell.accessoryView.hidden = NO;

}

UIImageView *cellAccessoryImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon-tick.png"]] ;
UIImageView *cellAccessoryNoneImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]] ;

if([self.arForIPs containsObject:indexPath]){
cell.accessoryView = cellAccessoryImageView;
} else {
cell.accessoryView = cellAccessoryNoneImageView;
}

// Get item from tableData
NSDictionary *item = (NSDictionary *)[_arForTable objectAtIndex:indexPath.row];

// encoding fix
NSString *utf8StringTitle = [item objectForKey:@"name"];

NSString *correctStringTitle = [NSString stringWithCString:[utf8StringTitle cStringUsingEncoding:NSISOLatin1StringEncoding] encoding:NSUTF8StringEncoding];

cell.textLabel.text = [correctStringTitle capitalizedString];

NSNumber *num = [item objectForKey:@"id"];

cell.detailTextLabel.text = [num stringValue];

cell.detailTextLabel.hidden = YES;

return cell;
}

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

if([self.arForIPs containsObject:indexPath]){
[self.arForIPs removeObject:indexPath];

[self.categorySelected removeObject:[[self.arForTable objectAtIndex:indexPath.row] objectForKey:@"id"]];

} else {
[self.arForIPs addObject:indexPath];

[self.categorySelected addObject:[[self.arForTable objectAtIndex:indexPath.row] objectForKey:@"id"]];

NSLog(@"%@ categorySelected",self.categorySelected);

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSLog(@"%@ defaults categorySelected",[defaults arrayForKey:@"selectedCategoryTableString"]);

NSString *string = [self.categorySelected componentsJoinedByString:@","];

[defaults setObject:string forKey:@"selectedCategoryTableString"];

NSLog(@"%@ STRING",string);

}

[tableView reloadData];
}



-(void) viewWillAppear:(BOOL)animated {

[super viewWillAppear:NO];

[self.navigationController setNavigationBarHidden:YES animated:NO];

self.navigationController.toolbarHidden = YES;

}

最佳答案

首先,您的代码有很多内存泄漏,请务必使用静态分析器和/或工具来修复它们,它们很少是非常明显的,例如您初始化了 SBJSON 解析器但没有释放它,itemsTMP 是另一个。

我重写了您的代码,使其更加高效和内存友好:

@interface CategoriesViewController : UITableViewController
{
NSArray *_items;
NSMutableArray *_selectedItems;

UIImageView *cellAccessoryImageView;
}

@end



@implementation CategoriesViewController

- (void)viewDidLoad
{
[super viewDidLoad];

_selectedItems = [NSMutableArray new];

cellAccessoryImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon-tick.png"]] ;

[self reloadMain];

self.tableView.allowsMultipleSelection = YES;
}

- (void)reloadMain
{
NSString *jsonString = @"http:///******";

// Download the JSON
jsonString = [NSString
stringWithContentsOfURL:[NSURL URLWithString:jsonString]
encoding:NSStringEncodingConversionAllowLossy|NSUTF8StringEncoding
error:nil];

// Create parser
SBJSON *parser = [SBJSON new];
NSDictionary *results = [parser objectWithString:jsonString error:nil];

if (_items) [_items release];
_items = [[results objectForKey:@"results"] copy];

[parser release];

[self.tableView reloadData];
}

#pragma mark - Table view data source

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

- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_items count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

[cell.textLabel setFont:[UIFont fontWithName: @"Asap-Bold" size: 14.0f]];
[cell.detailTextLabel setFont:[UIFont fontWithName: @"Asap-Bold" size: 14.0f]];
cell.accessoryView.hidden = NO;

}

NSDictionary *item = [_items objectAtIndex:indexPath.row];

if ([_selectedItems containsObject:item])
{
// preloaded image will help you have smoother scrolling
cell.accessoryView = cellAccessoryImageView;
}
else
{
cell.accessoryView = nil;
cell.accessoryType = UITableViewCellAccessoryNone;
}

// Get item from tableData
cell.textLabel.text = [[NSString stringWithCString:[[item objectForKey:@"name"] cStringUsingEncoding:NSISOLatin1StringEncoding] encoding:NSUTF8StringEncoding] capitalizedString];
cell.detailTextLabel.text = [[item objectForKey:@"id"] stringValue];
cell.detailTextLabel.hidden = YES;

item = nil;

return cell;
}

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

NSDictionary *item = [_items objectAtIndex:indexPath.row];
if ([_selectedItems containsObject:item])
{
[_selectedItems removeObject:item];
}
else
{
[_selectedItems addObject:item];
}
item = nil;

[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (void)dealloc
{
[_selectedItems release];
[cellAccessoryImageView release];

[super dealloc];
}

@end

关于iphone - View 消失后如何在 UITableView 中保留复选标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14882791/

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