簡體   English   中英

選中后永久更改tableView中的背景單元格

[英]Change background cell in tableView permanently after selected

如何在選擇后永久更改背景單元格? 我所知道的是通過使用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! ");
    }
}

您可以向數據源數組添加一個屬性,例如,名為BOOL isSelected in - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath您更新的- (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! ");
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM