我在另一个视图中有一个UITableView。每当我点击表格中的一个单元格时,我都希望该单元格被突出显示,直到再次单击以取消选择它为止。使用didSelectRowAtIndexPath方法,我已经完成了这一点。但是,细胞选择需要很长时间。在突出显示细胞之前,我必须坚持3秒钟,而不是瞬间。我怎样才能在它被触及的瞬间选择它?Swift:UITableViewCell选择中的延迟
这是我的相关代码。
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
}
}
我目前正在使用默认单元格,而不是自定义单元格。这将需要一个自定义单元吗?另外,也许我误解了这一点,但看起来这将在创建单元格后立即改变单元格的颜色。这不会改变它通过点击单元格选择单元格吗? – Jason247
这将单元格的“selectedBackgroundVIew”设置为“backgroundView”而不是“backgroundView”,因此它只会在突出显示和选择时出现......我将编辑我的答案以帮助您 – DBoyer
好吧,您选择单元格后它会改变颜色,但我仍然遇到与使用didSelectRowAtIndexPath时相同的问题。我必须让细胞保持3秒钟,以使细胞保持突出显示。 – Jason247