2013-07-08 78 views
4

自定義按鈕如何禁用UserInteractionUITableview細胞,但沒有在該單元格的自定義按鈕..如何禁用用戶交互的UITableView的細胞,但不是在細胞

我創造的一個按鈕:

UIButton *dayButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [dayButton setFrame:CGRectMake(201, 0, 30, 35)]; 
    [dayButton addTarget:self action:@selector(selectDayView) forControlEvents:UIControlEventTouchUpInside]; 
    [dayButton setBackgroundImage:[UIImage imageNamed:@"86-camera.png"] forState:UIControlStateNormal]; 
    dayButton.userInteractionEnabled=YES; 
    [cell addSubview:dayButton]; 

然後我設置

cell.userInteractionEnabled=NO; 

我怎樣才能得到dayButtonAction

回答

0

不可能爲UITableView的,而不是UITableViewCell適用禁止userInteraction。當啓用UITabelView時,您可以訪問UITableVie的內容/控制器(子視圖UITableView)。當您在UITableView添加任何控制器實際上你加它UITableViewCell,但電池是UITableView一部分,所以當你在UITableView禁用userInteraction這意味着它ASLO設置(NO)的UITableViewCell的。

這裏我ASLO給你建議你UIButton添加到細胞作爲

[cell.contentView addSubView:buttonName]; 
3

如果您設置了cell.userinteraction = NO,那麼您將無法觸摸按鈕,因爲cell及其subviews的任何交互均已關閉。

9

不要禁用用戶與整個單元格的交互。設置cell.selectionStyle = UITableViewCellSelectionStyleNone以防止單元格選擇。這樣用戶仍然可以與按鈕進行交互。

1

您可以設置[cell setSelectionStyle:UITableViewCellSelectionStyleNone];也沒有實現tblView:didSelectRowAtIndexPath:。讓你點擊按鈕(因爲它會默認)。處理它的事件來執行你的任務。

由於我明白如果上海華啓用了userintercation = no比它的子視圖不能互動。在你的情況下,UITableViewCell是超級視圖,按鈕是子視圖。所以最好避免設置cell.userInteractionEnabled = NO;並使用UITableViewCellSelectionStyleNone

希望這有助於:)

1

如果cell.userinteraction = NO則無法觸摸它的subviews。簡單的方法是隻要UIViewclearColour和視圖的大小是相同的你的單元格大小,並把它放在單元格和俯視圖上,你可以設置你的UIButton。當您想要cell.userinteraction = NO時,只需設置view.hidden=NO,然後cell.userinteraction = Yes然後設置view.hidden=YES。所以你的透明UIView顯示和以上查看您的UIButton然後你可以得到點擊按鈕。無需設置cell.userinteraction

0

您無法在單元上設置用戶交互禁用,因爲它是添加了按鈕的超級視圖。如果您這樣做,您將無法與按鈕交互,因此禁用UITableViewCell的其餘子視圖的交互。

2

一個古老的問題,但認爲這可能有助於某人。您可以通過將選擇屬性設置爲不選,禁用UITableView的用戶交互。

Identity Inspector

1

還有一個選項是重寫的touchesBegan和touchesEnded。具體方法如下:

1)使具有壓倒一切的功能和指標自定義單元格應用它們或不

class MyCustomTableViewCell: UITableViewCell { 
    var shouldDisableTouches: Bool = false 

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     if !shouldDisableTouches { super.touchesBegan(touches, with: event) } 
    } 

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
     if !shouldDisableTouches { super.touchesEnded(touches, with: event) } 
    } 
} 

2)故事板設置你的類這個類中定義類>類

3)現在的tableView(_:cellForRowAt :)你可以打開或關閉

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "yourCellIdentifier", for: indexPath) as! MyCustomTableViewCell 
    if <your condition here> { 
     cell.shouldDisableTouches = true 
    } else { 
     cell.shouldDisableTouches = false 
    } 
} 

互動......在「其他」部分是必不可少這裏,不要放棄它。畢竟它是一個可重用的單元。

這種禁用不會影響位於單獨的xib中的按鈕,該按鈕用於註冊此單元格。還沒有在單元上測試過它,直接在故事板中設計。