2015-06-24 101 views
0

我在另一个视图中有一个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 
    } 
} 

回答

1

这应该可以工作,但它会更加清洁,使自定义单元格的子类。

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

    let cell = UITableViewCell(style: UITableViewCellStyle.Value2, reuseIdentifier: "addDataCell") 

    cell.textLabel!.text = "Test" 

    let backgroundView = UIView() 
    backgroundView.backgroundColor = YOUR_COLOR 
    cell.selectedBackgroundView = backgroundView 

    return cell 
} 

删除,因为我们希望小区选择

cell.selectionStyle = UITableViewCellSelectionStyle.None 

删除,因为这已经是在细胞亚类的照顾

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() 
} 
+0

我目前正在使用默认单元格,而不是自定义单元格。这将需要一个自定义单元吗?另外,也许我误解了这一点,但看起来这将在创建单元格后立即改变单元格的颜色。这不会改变它通过点击单元格选择单元格吗? – Jason247

+0

这将单元格的“selectedBackgroundVIew”设置为“backgroundView”而不是“backgroundView”,因此它只会在突出显示和选择时出现......我将编辑我的答案以帮助您 – DBoyer

+0

好吧,您选择单元格后它会改变颜色,但我仍然遇到与使用didSelectRowAtIndexPath时相同的问题。我必须让细胞保持3秒钟,以使细胞保持突出显示。 – Jason247

1

的问题是UITapGestureRecognizer用的自来水干扰细胞。我很抱歉,轻拍手势代码不在我最初的帖子中,因为我没有意识到这可能是罪魁祸首。我已将它添加到原始帖子的代码段中。

+0

啊,是的,它会这样做:P ...很高兴你明白了! – DBoyer

+0

感谢您的跟进!这也是我的问题。我有一个轻击手势来解除键盘。一旦我评论说,敲出单元格就像冠军一样工作。 – Ponyboy