2013-11-20 21 views
0

我有一個自定義的子類UITableViewCell(下面的實現),當彈出一個子視圖控制器後沒有被取消選擇。我沒有使用UITableViewController。我正在使用其視圖包含tableView的UIViewController爲什麼在調用deselectRowAtIndexPath之後,我的自定義UITableViewCell保持選中狀態?

下面是相關viewWillAppear中試圖取消選定單元格:

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
    NSIndexPath *selection = [self.tableView indexPathForSelectedRow]; 
    if (selection) { 
     [self.tableView deselectRowAtIndexPath:selection animated:YES]; 
    } 
} 

我已經證實,self.tableView不爲零時,上面的代碼被調用。

定製UITableViewCell子類:

@implementation DSCaseCell 

- (id)initWithCoder:(NSCoder *)aDecoder 
{ 
    self = [super initWithCoder:aDecoder]; 
    if (self) { 
    } 
    return self; 
} 

- (void)setStatus:(NSString *)status 
{ 
    _status = status; 
    self.statusLabelView.backgroundColor = [DSStyles colorForCaseStatus:self.status]; 
    self.statusLabel.text = [self.status capitalizedString]; 
} 

- (void)layoutSubviews 
{ 
    if ([self.status isEqualToString:DSInteractionClassFacebookComment]) { 
     self.iconView.image = [UIImage iconForInteractionClass:self.status]; 
    } else { 
     self.iconView.image = [UIImage iconForInteractionClass:self.messageClass]; 
    } 

    self.statusLabel.font = [DSStyles preferredFontForTextStyle:DSFontTextStyleCaption2]; 
    self.statusLabelView.layer.cornerRadius = 3; 
    self.statusLabelView.backgroundColor = [DSStyles colorForCaseStatus:self.status]; 
    self.statusLabel.text = [self.status capitalizedString]; 
    self.statusLabel.textColor = [DSStyles invertedTextColor]; 
    self.statusLabel.highlightedTextColor = [DSStyles invertedTextColor]; 

    self.fromLabel.font = [DSStyles preferredFontForTextStyle:DSFontTextStyleLightCaption1]; 
    self.timeLabel.font = [DSStyles preferredFontForTextStyle:DSFontTextStyleLightCaption1]; 


    self.descriptionLabel.font = [DSStyles preferredFontForTextStyle:DSFontTextStyleLightCaption1]; 
    [super layoutSubviews]; 
} 

@end 
+0

indexPathForSelectedRow在viewWillAppear中返回非零值嗎? –

+0

是的,它是非零。我確認if子句正在評估爲true,並且deselectRowForIndexPath:animated被調用。 –

+0

我不知道viewWillAppear在視圖管道中是否太早。你可以嘗試viewDidAppear,看看是否有用? –

回答

0

看來這是由以下代碼tableView:didSelectRowAtIndexPath:

UITableViewCell *selectedCell = [self tableView:self.tableView cellForRowAtIndexPath:indexPath]; 

它更改爲這引起了固定的問題:

UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath]; 

不確定我在第一次嘗試時喝的是什麼。

相關問題