didSelectRowAtIndexPath called after long press

半腔热情 提交于 2019-12-09 03:42:52

问题


I have a uitableview right below a uitextfield that populates similar strings in a uitableview cell. The items get populated. However the didSelectRowAtIndexPath gets called after long press

My UITableView (is embedded in UIScrollView) - didSelectRowAtIndexPath: only being called after long press on custom cell

My view hierarchy:

  • UIScrollView (outerscroll)
    • Some other views and buttons
    • UITexfield
    • UITableView (tableView)

My Code

class MyViewController:  BaseViewController , UITextFieldDelegate , UITableViewDelegate , UITableViewDataSource , UIGestureRecognizerDelegate  {



@IBOutlet weak var autocompleteTableView: UITableView!
var pastUrls = ["Men", "Women", "Cats", "Dogs", "Children","aaaaaaaaa","aaaaaaaaaaaaaaaaaaa","aaaaaaaaa","a","aa","aaa"]
var autocompleteUrls = [String]()

@IBOutlet weak var image_view_seacrh_ifsc: UIImageView!


@IBOutlet weak var scrollView: UIScrollView!


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    self.scrollView.panGestureRecognizer.delaysTouchesBegan = true

    autocompleteTableView.delegate = self
    autocompleteTableView.dataSource = self
    autocompleteTableView.scrollEnabled = true
    autocompleteTableView.alwaysBounceVertical = false
    autocompleteTableView.allowsSelection = true
    autocompleteTableView.allowsSelectionDuringEditing = true

    autocompleteTableView.hidden = true




    super.hideKeyboard()
    super.showNavigationBarBackButton()

    let gesture_search_ifsc = UITapGestureRecognizer(target: self, action: "action_Search_Ifsc:")
    gesture_search_ifsc.delegate = self
    gesture_search_ifsc.numberOfTapsRequired = 1

    image_view_seacrh_ifsc.userInteractionEnabled = true
    image_view_seacrh_ifsc.addGestureRecognizer(gesture_search_ifsc)


}




func searchAutocompleteEntriesWithSubstring(substring: String)
{
    autocompleteUrls.removeAll(keepCapacity: false)

    for curString in pastUrls
    {
        var myString:NSString! = curString as NSString

        var substringRange :NSRange! = myString.rangeOfString(substring)

        if (substringRange.location  == 0)
        {
            autocompleteUrls.append(curString)
        }
    }

    autocompleteTableView.reloadData()
    //autocompleteTableView.hidden = false
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return autocompleteUrls.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


    let cell: AutoBankCell = tableView.dequeueReusableCellWithIdentifier("AutoBankCell") as! AutoBankCell

    cell.label.text = self.autocompleteUrls[indexPath.row]
    //cell.lbl.text = self.autocompleteUrls[indexPath.row]

    return cell


}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let selectedCell : UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
    self.bank_name.text = self.autocompleteUrls[indexPath.row]

    self.autocompleteTableView.scrollEnabled = true
    self.autocompleteTableView.hidden = true
}


}

My UItableView gets populated and works properly when not embedded in UIScrollView.


回答1:


Try to add uiview in your scroll view and add every elements in that uiview like your tableView,textfield,views and buttons etc.

So, that view works as container view.

Make sure that you are setting proper constraints if you are using autolayout.

Second thing remove unnecessary gesture recognizer implicitly which is not required.

Tableview have scroll enable by default so not need to set it true again. (it's not affect your issue btw!!)

So remove unnecessary configuration and make proper setup and your issue will be solved i think.



来源:https://stackoverflow.com/questions/38217789/didselectrowatindexpath-called-after-long-press

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!