[英]tableView:didSelectRowAtIndexPath not called in custom view
我做了一個popupView
在項目中選擇一些對象。
除了未調用tableViewDelegate
以外,其他所有方法都正常工作。( DataSource
正常工作)
我從cellForRowAtIndexPath
記錄了tableView.delegate
的值。它也具有正確的值。
下面是我的代碼。
protocol AddressSelectPopupDelegate: NSObjectProtocol {
func addressSelectPopup(selectedContact: Contact) }
class AddressSelectPopupView: UIView, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
weak var parentView: UIView!
weak var delegate: AddressSelectPopupDelegate!
var contactList : [Contact] = []
var selectedContact : Contact?
class func addressSelectPopupView(parentView: UIView, delegate: AddressSelectPopupDelegate) -> AddressSelectPopupView
{
var popupView = UINib(nibName: "AddressSelectPopupView", bundle: NSBundle.mainBundle()).instantiateWithOwner(nil, options: nil)[0] as! AddressSelectPopupView
popupView.parentView = parentView
popupView.delegate = delegate
popupView.tableView.registerNib(UINib(nibName: "AddressInfoTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier:"AddressInfoTableViewCell")
popupView.tableView.delegate = popupView
popupView.tableView.dataSource = popupView
return popupView
}
//MARK: - Public Interface
func popup()
{
self.parentView.addSubview(self)
self.loadContactList()
}
func dismiss()
{
self.removeFromSuperview()
}
//MARK: - Private Interface
private func loadContactList()
{
ConnectionManager.connection.loadContactList({ [weak self](contactList) -> Void in
self!.contactList = contactList
dispatch_async(dispatch_get_main_queue(), { () -> Void in
if self!.tableView != nil
{
self!.tableView.reloadData()
}
})
}, failure: { [weak self] () -> Void in
self!.tableView.reloadData()
})
}
//MARK: - UITableView Delegate & DataSource
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return contactList.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:AddressInfoTableViewCell = tableView.dequeueReusableCellWithIdentifier("AddressInfoTableViewCell") as! AddressInfoTableViewCell
let contact = contactList[indexPath.row]
cell.contact = contact
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedContact = contactList[indexPath.row]
}
func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
selectedContact = contactList[indexPath.row]
return indexPath
}
@IBAction func cancelAction(sender: AnyObject) {
self.dismiss()
}
@IBAction func confirmAction(sender: AnyObject) {
//TODO : add confirmAction
if selectedContact != nil
{
self.dismiss()
if delegate.respondsToSelector(Selector("addressSelectPopup:"))
{
delegate.addressSelectPopup(selectedContact!)
}
}
}}
///更新1
我使用上面的Delegate&DataSource代碼創建了一個新的ViewController(稱為PopupVC)。
方式1:我從rootView呈現PopupVC。
結果:它工作正常。
方式2:我將PopupVC的視圖作為子視圖添加到rootView,還將PopupVC作為childViewController添加到rootView。
結果:滑動單元格將調用didSelectRowAtIndexPath
。 點擊只會使該單元格突出顯示。
///更新2
我只是注意到rootView是由另一個View呈現的。
首先刪除子視圖的點擊手勢,然后再調用popoverview 。 然后僅在popoverview內部調用UITableViewDelegate
。 要從您的視圖中刪除手勢,請使用以下代碼:
self.view.gestureRecognizers?.forEach(self.view.removeGestureRecognizer(_:))
self.view.addSubview(self.customView.view)
將此添加到您的班級:
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
tableView.delegate = self
tableView.dataSource = self
}
好吧,您不應該在接口構建器中或以編程方式連接委托和數據源。 我相信其中一個設置得不好,並且會干擾另一個。
您可以做的是嘗試在界面生成器中刪除鏈接,檢查發生了什么,如果不起作用,請再次添加它們,然后在代碼中刪除鏈接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.