1

我有一个 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;

}
4

2 回答 2

1

因为在您的表格中只有一个部分。试试这种方法,这肯定会帮助你。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 编写以下代码;

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 编写如下代码时,

       if([self.arForIPs containsObject:[NSNumber numberWithInt:indexPath.row]]){
            [self.arForIPs removeObject:[NSNumber numberWithInt:indexPath.row]];
        } else {
            [self.arForIPs addObject:[NSNumber numberWithInt:indexPath.row]]
        }
于 2013-02-14T20:24:15.393 回答
1

首先,您的代码有很多内存泄漏,请务必使用静态分析器和/或工具来修复它们,对于它们来说很少有像您初始化 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
于 2013-02-14T22:17:25.287 回答