我在另一个 View 中有一个 UITableView。每当我单击表格中的一个单元格时,我希望该单元格突出显示,直到再次单击它以取消选择它。使用 didSelectRowAtIndexPath 方法,我已经完成了这个。但是,小区选择需要很长时间。我必须按住一个单元格 3 秒钟才能突出显示该单元格,而不是瞬间。如何让它在触摸时立即选择单元格?
这是我的相关代码。
class AddDataViewController : UIViewController {
@IBOutlet weak var locationTableView: UITableView!
var fstViewController : FirstViewController?
let locationTableViewController = LocationTableViewController()
override func viewWillAppear(animated: Bool) {
// Set the data source and delgate for the tables
self.locationTableView.delegate = self.locationTableViewController
self.locationTableView.dataSource = self.locationTableViewController
// Set the cell separator style of the tables to none
self.locationTableView.separatorStyle = UITableViewCellSeparatorStyle.None
// Refresh the table
self.locationTableView.reloadData()
}
override func viewDidLoad(){
super.viewDidLoad()
// Create a tap gesture recognizer for dismissing the keyboard
let tapRecognizer = UITapGestureRecognizer()
// Set the action of the tap gesture recognizer
tapRecognizer.addTarget(self, action: "dismissKeyboard")
// Add the tap gesture recognizer to the view
//self.view.addGestureRecognizer(tapRecognizer)
}
}
class LocationTableViewController : UITableViewController, UITableViewDelegate, UITableViewDataSource {
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return tableView.frame.height / 5
}
override func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
override func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath) {
tableView.cellForRowAtIndexPath(indexPath)?.backgroundColor = UIColor.greenColor()
}
override func tableView(tableView: UITableView, didUnhighlightRowAtIndexPath indexPath: NSIndexPath) {
tableView.cellForRowAtIndexPath(indexPath)?.backgroundColor = UIColor.whiteColor()
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.cellForRowAtIndexPath(indexPath)?.backgroundColor = UIColor.blueColor()
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Value2, reuseIdentifier: "addDataCell")
cell.selectionStyle = UITableViewCellSelectionStyle.None
cell.textLabel!.text = "Test"
return cell
}
}
最佳答案
问题是 UITapGestureRecognizer 干扰了单元格的点击。我很抱歉点击手势代码不在我最初的帖子中,因为我没有意识到这可能是罪魁祸首。我已将其添加到原始帖子的代码片段中。
关于ios - Swift:延迟 UITableViewCell 选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31016244/