2

うまく機能するテーブルビューがあります。唯一のことは、セルが選択されているときに強調表示されたままになることです。そのため、セルをタップして別のビューに移動し、ナビゲーション バーの戻るボタンを押してテーブル ビューに戻ると、選択したセルがまだ強調表示されます。どうすればこれを修正できますか?

#import "SocialNetworks.h"
#import "Facebook.h"
#import "Twitter.h"
#import "YouTube.h"
#import "CustomCellBackground.h"

@interface SocialNetworks ()

@end

@implementation SocialNetworks
{
    NSMutableArray *mbTableData;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    mbTableData = [NSMutableArray arrayWithObjects:@"Facebook", @"Twitter", @"YouTube", nil];
    self.title = @"Social Networks";
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [mbTableData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *mbTableIdentifier = @"SimpleTableItem";
    UIImageView *image = [[UIImageView alloc]init];
    image.image = [UIImage imageNamed:@"CellImage.png"];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:mbTableIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:mbTableIdentifier];
        cell.textLabel.font=[UIFont systemFontOfSize:16.0];
    }

    // cell.backgroundView = [[CustomCellBackground alloc] init];
    // cell.selectedBackgroundView = [[CustomCellBackground alloc] init];
    cell.textLabel.font = [UIFont fontWithName:@"FranklinGothicStd-ExtraCond" size:24.0];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.textLabel.highlightedTextColor = [UIColor darkGrayColor];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.backgroundView = image;

    NSLog(@"cell separator style: %d separator color: %@", tableView.separatorStyle, tableView.separatorColor);

    cell.textLabel.text = [mbTableData objectAtIndex:indexPath.row];
    return cell;
}

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

    if (indexPath.row == 0)
    {
        Facebook *facebook = [[Facebook alloc] initWithNibName:@"Facebook" bundle:[NSBundle  mainBundle]];
        [self.navigationController pushViewController:facebook animated:YES];
    }
    else if (indexPath.row == 1)
    {
        Twitter *twitter = [[Twitter alloc] initWithNibName:@"Twitter" bundle:[NSBundle  mainBundle]];
        [self.navigationController pushViewController:twitter animated:YES];
    }
    else if (indexPath.row == 2)
    {
        YouTube *youTube = [[YouTube alloc] initWithNibName:@"YouTube" bundle:[NSBundle  mainBundle]];
        [self.navigationController pushViewController:youTube animated:YES];
    }


}


@end

編集:質問を投稿した直後にそれを理解しました。

[tableView deselectRowAtIndexPath:indexPath animated:YES];
4

2 に答える 2

14

行のdidSelectRowAtIndexPath選択を解除するための呼び出しを行うだけです

    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
于 2013-05-20T19:14:58.197 に答える
1

いつでも使用できます:

[tableView deselectRowAtIndexPath:indexPath animated:YES];

tableView:didSelectRowAtIndexPath: メソッドで。

于 2013-05-20T19:15:28.923 に答える