简体   繁体   English

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

[英]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. 我所知道的是通过使用cell.selectedBackgroundView它只会改变一段时间的单元格背景,然后它会恢复正常。

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: 这是我的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;
    }

below are my didSelectRowAtIndexPath method; 下面是我的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! ");
    }
}

You can add one property to your datasource array, for example, named BOOL isSelected . 您可以向数据源数组添加一个属性,例如,名为BOOL isSelected in - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath you update isSelected = YES . in - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath您更新的- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 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 在你的- (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] ];
 }

the code is here: 代码在这里:

You need add one bool property to the object in your array. 您需要将一个bool属性添加到数组中的对象。 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! ");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM