didSelectRowAtIndexPath called after long press

2020-04-10 03:35发布

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条回答
男人必须洒脱
2楼-- · 2020-04-10 04:33

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.

查看更多
登录 后发表回答