user2767343
user2767343

Reputation: 173

Change background cell in tableView permanently after selected

How can I change the background cell permanently after being selected? What I know is by using cell.selectedBackgroundView it will only change the cell background for a while and then it goes back to normal.

I want it to change permanently when selected even after app are closed. I tried to search for a solution but could not find relevant answer.

this is my cellForRowAtIndexPath method:

 - (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;
    }

below are my didSelectRowAtIndexPath method;

- (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! ");
    }
}

Upvotes: 0

Views: 1007

Answers (1)

chancyWu
chancyWu

Reputation: 14403

You can add one property to your datasource array, for example, named BOOL isSelected. in - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath you update isSelected = YES. and add the following

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

to update the selected cell. and in your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method

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

the code is here:

You need add one bool property to the object in your array. For example,you add

@property (nonatomic) BOOL isSelected; to your object class's header file.

- (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! ");
    }
}

Upvotes: 2

Related Questions