选中后永久更改tableView中的背景单元格

时间:2013-11-20 01:43:14

标签: ios objective-c uitableview

如何在选择后永久更改背景单元格?我所知道的是使用cell.selectedBackgroundView它只会改变细胞背景一段时间然后又恢复正常。

我希望即使在关闭应用后,也会选择永久更改。我试图寻找解决方案但找不到相关的答案。

这是我的cellForRowAtIndexPath方法:

 - (UITableViewCell *)tableView:(UITableView *)tableView2 cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView2 dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];

//set up selected cell background
        cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"white_selected.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];
        cell.selectedBackgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"blue_selected.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];

        }

        //set up cell
        CGFloat nRed= 0/255.0;
        CGFloat nGreen=73.0/255.0;
        CGFloat nBlue=144.0/255.0;
        UIColor *myColor=[[UIColor alloc]initWithRed:nRed green:nGreen blue:nBlue alpha:1];

//set up cell text
        cell.textLabel.text = [[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"title"];
        cell.textLabel.highlightedTextColor = [UIColor colorWithRed:51.0f/255.0f green:102.0f/255.0f blue:153.0f/255.0f alpha:1.0f];
        cell.textLabel.font=[UIFont fontWithName:@"HelveticaNeue-Bold" size:15];
        cell.textLabel.textColor=myColor;
        cell.backgroundColor = [UIColor colorWithRed: 0.0 green: 0.0 blue: 0.2 alpha: 1.0];

        //set icon image for cell
        cell.imageView.image = [UIImage imageNamed:
                              [[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"icon"]];

        return cell;
    }

以下是我的didSelectRowAtIndexPath方法;

- (void)tableView:(UITableView *)tableView2 didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    indexPath];
    [self->tableView deselectRowAtIndexPath:indexPath animated:YES];

    if ([[[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"action"] isEqualToString:@"a1"]){

        NSString *title_a1 =[[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"title"];
        NSString *content_a1 = [[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"content"];
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

        // saving an NSString
        [defaults setObject:title_a1 forKey:@"title_a1"];
        [defaults setObject:content_a1 forKey:@"content_a1"];

        WebBrowserViewController *webview=[[WebBrowserViewController alloc]initWithNibName:@"WebBrowserViewController" bundle:nil];
            [self presentModalViewController:webview animated:YES];

    }else  if ([[[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"action"] isEqualToString:@"a2"]){

        NSString *title_a2 =[[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"title"];
        NSString *content_a2 = [[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"content"];
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

        // saving an NSString
        [defaults setObject:title_a2 forKey:@"title_a2"];
        [defaults setObject:content_a2 forKey:@"content_a2"];

        WebBrowserViewController *webview=[[WebBrowserViewController alloc]initWithNibName:@"WebBrowserViewController" bundle:nil];
            [self presentModalViewController:webview animated:YES];

    }else {

        NSLog(@" other action key get! ");
    }
}

1 个答案:

答案 0 :(得分:2)

您可以向数据源数组添加一个属性,例如,名为BOOL isSelected。 在- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath中,您更新了isSelected = YES。并添加以下

[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[tableView endUpdates];

更新所选单元格。并在您的- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法

if(data.isSelected){
   cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"blue_selected.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
 }

代码在这里:

您需要向数组中的对象添加一个bool属性。例如,您添加

@property (nonatomic) BOOL isSelected;到对象类的头文件。

- (UITableViewCell *)tableView:(UITableView *)tableView2 cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView2 dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];

//set up selected cell background
        //cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"white_selected.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];
        //cell.selectedBackgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"blue_selected.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];

        }

        if (((YourObjectType *)[AryStoreknowItem objectAtIndex:indexPath.row]).isSelected){
            cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"blue_selected.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];
        } else {
             cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"white_selected.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];

        }

        //set up cell
        CGFloat nRed= 0/255.0;
        CGFloat nGreen=73.0/255.0;
        CGFloat nBlue=144.0/255.0;
        UIColor *myColor=[[UIColor alloc]initWithRed:nRed green:nGreen blue:nBlue alpha:1];

//set up cell text
        cell.textLabel.text = [[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"title"];
        cell.textLabel.highlightedTextColor = [UIColor colorWithRed:51.0f/255.0f green:102.0f/255.0f blue:153.0f/255.0f alpha:1.0f];
        cell.textLabel.font=[UIFont fontWithName:@"HelveticaNeue-Bold" size:15];
        cell.textLabel.textColor=myColor;
        cell.backgroundColor = [UIColor colorWithRed: 0.0 green: 0.0 blue: 0.2 alpha: 1.0];

        //set icon image for cell
        cell.imageView.image = [UIImage imageNamed:
                              [[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"icon"]];

        return cell;
    }


    - (void)tableView:(UITableView *)tableView2 didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    indexPath];
    [self->tableView deselectRowAtIndexPath:indexPath animated:YES];

    ((YourObjectType *)[AryStoreknowItem objectAtIndex:indexPath.row]).isSelected = YES;

    [tableView beginUpdates];
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    [tableView endUpdates];

    if ([[[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"action"] isEqualToString:@"a1"]){

        NSString *title_a1 =[[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"title"];
        NSString *content_a1 = [[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"content"];
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

        // saving an NSString
        [defaults setObject:title_a1 forKey:@"title_a1"];
        [defaults setObject:content_a1 forKey:@"content_a1"];

        WebBrowserViewController *webview=[[WebBrowserViewController alloc]initWithNibName:@"WebBrowserViewController" bundle:nil];
            [self presentModalViewController:webview animated:YES];

    }else  if ([[[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"action"] isEqualToString:@"a2"]){

        NSString *title_a2 =[[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"title"];
        NSString *content_a2 = [[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"content"];
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

        // saving an NSString
        [defaults setObject:title_a2 forKey:@"title_a2"];
        [defaults setObject:content_a2 forKey:@"content_a2"];

        WebBrowserViewController *webview=[[WebBrowserViewController alloc]initWithNibName:@"WebBrowserViewController" bundle:nil];
            [self presentModalViewController:webview animated:YES];

    }else {

        NSLog(@" other action key get! ");
    }
}