如何在重新加载后永久地在表格单元格中设置图像?

时间:2013-12-08 13:33:35

标签: uitableview uiimage didselectrowatindexpath

我测试用户是否点击了一个单元格。如果单击它,图像会更改。即使用户再次单击该图像或重新加载应用程序,我也希望该图像保持选中状态。

这真的很接近工作。现在,当选择单元格时,图像从灰色版本变为绿色版本。问题是当用户再次单击单元格时,灰色图像会显示备份。我需要做些什么来解决这个问题?

 //begin checking for selected row and add checkmark
- (NSString *)getKeyForIndex:(int)index
{
    return [NSString stringWithFormat:@"KEY%d",index];
}

- (BOOL) getCheckedForIndex:(int)index
{
    if([[[NSUserDefaults standardUserDefaults] valueForKey:[self getKeyForIndex:index]] boolValue]==YES)
    {
        return YES;
    }
    else
    {
        return NO;
    }
}



- (void) checkedCellAtIndex:(int)index
{
    BOOL boolChecked = [self getCheckedForIndex:index];

    [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:!boolChecked] forKey:[self getKeyForIndex:index]];
    [[NSUserDefaults standardUserDefaults] synchronize];
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tableList count];
}


// Customize the content and the look of table view cells.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";
    UIImage *greyck =[UIImage imageNamed:@"greycheck-sd.png"];
    UIImage *greenck =[UIImage imageNamed:@"greencheck-sd.png"];

    //step 1 check to see if we can reuse a cell that has just rolled off the screen
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath: indexPath];

    //step 2: if there are no cells to be reused, create a new cell
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }

    //step 3: set the text in the cell using items from the array
    cell.textLabel.text  = [tableList objectAtIndex:indexPath.row];

    //set custom font
    cell.textLabel.font = [UIFont fontWithName:@"Chalkboard SE" size:18.0f];


    //check for previously viewed then set the image
    if([self getCheckedForIndex:indexPath.row]==YES)
    {
        //sets green checkmark after user clicks
        cell.imageView.image = greenck;
    }
    else
    {
        cell.imageView.image = greyck;
    }

    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIImage *greyck =[UIImage imageNamed:@"greycheck-sd.png"];
    UIImage *greenck =[UIImage imageNamed:@"greencheck-sd.png"];

    NSString *str= [tableList objectAtIndex:indexPath.row];
    if ([str isEqual:@"introduction"])
    {
        NSBundle *bundle = [NSBundle mainBundle];
        NSString *moviePath = [bundle pathForResource:@"Step1-Intro" ofType:@"mp4"];
        NSURL *movieURL = [NSURL fileURLWithPath:moviePath];

        MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
        theMovie.scalingMode = MPMovieScalingModeAspectFill;
        [theMovie play];
        MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
        [self presentMoviePlayerViewControllerAnimated:moviePlayer];

    }

    else if ([str isEqual:@"skating"])
    {
        NSBundle *bundle = [NSBundle mainBundle];
        NSString *moviePath = [bundle pathForResource:@"Step2-Skating" ofType:@"mp4"];
        NSURL *movieURL = [NSURL fileURLWithPath:moviePath];

        MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
        theMovie.scalingMode = MPMovieScalingModeAspectFill;
        [theMovie play];
        MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
        [self presentMoviePlayerViewControllerAnimated:moviePlayer];
    }




    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    //Use checkedCellAtIndex for check or uncheck cell
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    [self checkedCellAtIndex:indexPath.row];

    if([self getCheckedForIndex:indexPath.row]==YES)
    {
        cell.imageView.image = greenck;
    }
    else
    {
        cell.imageView.image = greyck;
    }

}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

1 个答案:

答案 0 :(得分:0)

您可以使用

将显示的图像设置为2个位置的绿色
   if([self getCheckedForIndex:indexPath.row]==YES)
    {
        cell.imageView.image = greenck;
    }

getCheckedForIndex:会返回YES

if([[[NSUserDefaults standardUserDefaults] valueForKey:[self getKeyForIndex:index]] boolValue]==YES)  

到目前为止,这么好。但在我看来,你永远不会更新你的用户默认值。如果是这样,你总是会返回相同的值,什么都不会改变。