2013-05-20 100 views
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]; 

回答

14
didSelectRowAtIndexPath

你只需要撥打電話取消選擇行

[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; 
+0

以下是swift版本: 'tableView.deselectRowAtIndexPath(indexPath,animated:true)' – AziCode

1

您可以隨時使用:

[tableView deselectRowAtIndexPath:indexPath animated:YES]; 
中的tableView

:didSelectRowAtIndexPath方法:方法。

相關問題