ios - 表格 View 单元格保持突出显示

标签 ios uitableview

我有一个工作正常的 TableView 。唯一的问题是当一个单元格被选中时,它会保持突出显示。所以,如果我点击一个单元格转到另一个 View 并点击导航栏上的后退按钮返回到表格 View ,我选择的单元格仍然突出显示。我该如何解决这个问题?

#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];

最佳答案

didSelectRowAtIndexPath 中,您只需调用取消选择行

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

关于ios - 表格 View 单元格保持突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16656645/

相关文章:

iphone - 自动化Localizable.strings?

iphone - 如何使用 NSMutableDictionary 键作为单元格标题来填充 UITableView?

objective-c - 如何在同一个 UITableViewCell 中显示两行?

iphone - 如何设置表格 View 单元格适应标签?

ios - 错误的NSLayoutConstraint导致黑屏

ios - 具有固定列数和动态单元格高度的 UICollectionView

ios - 快速获取 json 响应中的计数(项目数)

javascript - 阻止 UIWebView 缓存 JavaScript 文件?

ios - 当我单击 uitableview 单元格时,该单元格的文本变得更粗?

swift - 在 Swift 中使用纹理(AsyncDisplayKit),当我滚动经过 ASTableNode 中最后一个 ASCellNode 的底部时,如何防止闪烁?