[英]How to detect tap on Uitableview cell with uiview and uibutton?
我創建了一個表格視圖,並將自定義uiview添加到單元格的內容視圖中。 uiview有一個uibutton。 但是,我可以在創建單元格時將uibutton添加到內容視圖中。
我想在tableview上獲取tap事件以執行一些操作。 我還希望uibutton上的tap事件執行其他操作。
問題是當我點擊一行時,按鈕也被按下,並且觸發了兩個事件。 我如何謹慎地獲得兩個事件? 即,當單擊uibuttons邊界之外的單元格部分時,單擊tableview單元格。
您可以按以下方式在單元格中添加按鈕
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake( 13.0f, 13.0f, 90.0f, 90.0f)];
[button addTarget:self action:@selector(BtnPressed) forControlEvents:UIControlEventTouchUpInside];
[cellView.contentView addSubview:button];
然后您可以分別獲取事件
您確定同一點擊會觸發兩個不同的事件嗎? 我的理解是,UIKit僅生成一個UIEvent並將其發送到響應該手勢類型的層次結構中的最頂層視圖。 在這種情況下,如果按鈕在視圖層次結構中位於較高位置,則它可能正在接收事件消息。 但是,我可能錯了。
絕對避免觸發兩個事件(盡管可能不是理想的)的一種解決方案是停用tableView的行選擇,如下所示:
self.tableView.allowsSelection = NO;
然后,添加一個覆蓋tableCell其余部分的視圖,並向該視圖添加一個手勢識別器。 由於它們不在同一地區,因此沒有發生沖突事件的機會。 當然,要知道點擊了哪一行,您必須為按鈕和新視圖都添加一個實例變量來保存indexPath。 在tableView:cellForRowAtIndexPath中設置tableCell時,將設置索引路徑:
希望這對您有所幫助或為您提供一些新的想法。
您可以UITableViewCell
並添加一個按鈕,這是一個示例類:
import UIKit
private let DWWatchlistAddSymbolCellAddSymbolButtonLeftMargin: CGFloat = 15
class DWWatchlistAddSymbolCell: UITableViewCell {
private(set) var addSymbolButton:UIButton
init(reuseIdentifier:String?, primaryTextColor:UIColor) {
self.addSymbolButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
super.init(style:UITableViewCellStyle.Default, reuseIdentifier:reuseIdentifier)
selectionStyle = UITableViewCellSelectionStyle.None
backgroundColor = UIColor.clearColor()
imageView!.image = UIImage(named:"PlusIcon")!
addSymbolButton.setTitle("Add Symbol", forState:UIControlState.Normal)
addSymbolButton.setTitleColor(primaryTextColor, forState:UIControlState.Normal)
addSymbolButton.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left
addSymbolButton.contentVerticalAlignment = UIControlContentVerticalAlignment.Center
contentView.addSubview(addSymbolButton)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: Layout
override func layoutSubviews() {
super.layoutSubviews()
addSymbolButton.frame = contentView.frame
let imageViewFrame = imageView!.frame
let imageViewMaxX = imageViewFrame.origin.x + imageViewFrame.size.width
let addSymbolButtonX = imageViewMaxX + DWWatchlistAddSymbolCellAddSymbolButtonLeftMargin
addSymbolButton.contentEdgeInsets = UIEdgeInsetsMake(0, addSymbolButtonX, 0, 0);
}
}
您可以在底部看到“添加符號”按鈕:
當返回UITableViewDataSource
的單元格時,請確保設置按鈕的目標:
private func addButtonCell(tableView: UITableView) -> UITableViewCell {
var cell:DWWatchlistAddSymbolCell? = tableView.dequeueReusableCellWithIdentifier(kDWWatchlistViewControllerAddButtonCellReuseIdentifier) as? DWWatchlistAddSymbolCell
if (cell == nil) {
cell = DWWatchlistAddSymbolCell(reuseIdentifier:kDWWatchlistViewControllerAddButtonCellReuseIdentifier, primaryTextColor:primaryTextColor)
}
cell!.addSymbolButton.addTarget(self,
action:Selector("didPressAddSymbolButton"),
forControlEvents:UIControlEvents.TouchUpInside)
return cell!
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.