2013-02-07 52 views
0

我在每個單元格中都有一個UITableViewCellUIButton。當按下按鈕,我設置其狀態突出像this答案:無法將UIButton.highlighted設置爲NO

[self performSelector:@selector(doHighlight:) withObject:[cell.subviews objectAtIndex:2] afterDelay:0.0]; 

那麼我這樣做:

- (void)doHighlight:(UIButton *)sender { 

    if (sender.highlighted) { 
     [sender setHighlighted:NO]; 
    } else { 
    [sender setHighlighted:YES]; 
    } 
} 

但按鈕不僅只是沒有強調可言,不談論我應該能夠非常重視它的事實。

有什麼不對?

+0

是U確認[cell.subviews objectAtIndex:2]一個UIButton? – HRM

+0

@HRM是的,絕對是 –

+0

從哪個函數調用這個'performSelector'。在帖子中提到,當按下按鈕時,在那種情況下如何獲得'cell'實例? – HRM

回答

1

我結束了使用UIButtonselected財產。它不需要任何delay,並與這種類型的東西出色的作品:

if (!sender.selected) { 
    [sender setSelected:YES]; 
    [cell addSubview:hiddenButton]; 
    [self performSelector:@selector(doHighlight:) withObject:sender]; 
} else { 
    [sender setSelected:NO]; 
    [self performSelector:@selector(doUnHighlight:) withObject:sender]; 
} 
0

這是您正在尋找的一種可能的解決方案。我已經修改我的答案

UIButton *button=[cell.subviews objectAtIndex:2]; 

//I am adding these five lines to ensure the different 
//states of the button to achieve your highlighted state goal. 

UIImage *highlightImage = [UIImage imageNamed:@"highlight.png"];//Also used when button is selected 
UIImage *normalImage = [UIImage imageNamed:@"normal.png"]; 
[button setBackgroundImage:normalImage forState:(UIControlStateHighlighted)]; 
[button setBackgroundImage:highlightImage forState:(UIControlStateSelected)]; 
[button setBackgroundImage:normalImage forState:UIControlStateNormal]; 

[self performSelector:@selector(doHighlight:) withObject:[cell.subviews objectAtIndex:2]]; 

-(IBAction) doHighlight:(id)sender 
{ 
    if ([sender isKindOfClass:[UIButton class]]) 
    { 
     UIButton *btn=(UIButton*)sender; 
     if (btn.isSelected) { 
      [btn setSelected:NO]; 
     } 
     else 
     { 
      [btn setSelected:YES]; 
     } 
    } 
} 
+0

不幸的是,你錯了。使用這個「延遲」的東西有一個很大的意義。沒有它,該按鈕將不會突出顯示,直到再次點擊。即使我在決定時使用了「延遲」,它也沒有提供任何幫助。 –

相關問題