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

Uitableview 如何在重新加载后在表格单元格中永久设置图像?,uitableview,uiimage,didselectrowatindexpath,Uitableview,Uiimage,Didselectrowatindexpath,我对用户是否单击了单元格进行了测试。如果单击,图像将发生更改。我希望该图像保持选中状态,即使用户再次单击或应用程序重新加载 这真的很接近工作。现在,当选定单元格时,图像从灰色变为绿色。问题是,当用户再次单击单元格时,灰色图像会显示出来。我需要做什么来解决这个问题 //begin checking for selected row and add checkmark - (NSString *)getKeyForIndex:(int)index { return [NSString st

我对用户是否单击了单元格进行了测试。如果单击,图像将发生更改。我希望该图像保持选中状态,即使用户再次单击或应用程序重新加载

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

 //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

您可以在两个位置将显示的图像设置为绿色,并使用

   if([self getCheckedForIndex:indexPath.row]==YES)
    {
        cell.imageView.image = greenck;
    }
但是
getCheckedForIndex:
返回
YES

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

到目前为止,一切顺利。但在我看来,你从来没有更新过你的用户默认值。如果是这样,您将始终返回相同的值,并且不会有任何更改。

设置绿色或灰色复选标记取决于getCheckedForIndex:方法。所以,请发布您的代码,谢谢您的回复,Männer先生。适用代码张贴在上面。你想看看所有的代码吗?我没有使用附件指示器。我使用的是图像。cell.imageView.image=greenck;实际上,您没有发布方法“getCheckedForIndex:”的代码,但您的决定(绿色/灰色)取决于此。没有它,很难判断出什么是错的。我想你刚刚解决了我的问题。我试图在房间里给一个蹒跚学步的孩子编码,但忘记了我删除了它。现在,我忘了我做了什么。建议?(我对Objective-C非常陌生。请温柔一点。)如果我理解你的权利,你就删除了你仍然需要的代码。如果您在XCode中使用源代码管理(非常推荐),并且您曾经提交过您的应用程序,那么这没有问题:只需打开您的版本编辑器,它就会显示较旧的版本。如果您碰巧没有使用源代码管理,您仍然可以尝试从类似时间机器的备份中恢复旧版本。祝你好运先生,我不明白。我应该用上面的NSUserDefailts代码替换其中一个测试吗?我只测试了一个if/else,但它根本无法运行。我想说的是,您的方法“getCheckedForIndex:”返回YES或NO,具体取决于您的UserDefaults设置。但在我看来,你永远不会改变这些设置。如果是这样,您的图像将始终设置为相同的颜色。它会在选择时更改颜色。再次单击时,它会变回原来的状态。如何设置它以使其保持绿色?我希望我可以发布一个图像或视频。如果我理解正确,我需要一个新的功能,在用户观看将图像设置为绿色的视频后,设置默认用户设置。我怎么做呢?我不确定我是否正确,但我的印象是,您应该根据适当设置的实例变量而不是用户默认值(应该相当稳定)来决定使用哪种颜色。