2017-02-14 52 views
0

我有一个分组样式表视图。而我想要实现的是从手指着陆屏幕的那一刻起突出其细胞。但是实际上做的是在触及时突出显示。这是迄今为止我所拥有的。如何向UITableViewCell添加触摸识别器?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    var cell = UITableViewCell() 
    cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 
    cell.textLabel?.text = labels[indexPath.section][indexPath.row] 
    cellSetup(cell: cell) 
    return cell 
} 

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
    cell.prepareDisclosureIndicator() 
} 

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { 
    let header = view as! UITableViewHeaderFooterView 
    header.textLabel?.font = header.textLabel?.font.withSize(12) 
    header.textLabel?.textColor = ColorConstants.symbolsColor 
} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    if indexPath.section == 1 { 
     print("\(indexPath.row) didSelectRowAt") 
    } 
    let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath)! 
    selectedCell.backgroundColor = ColorConstants.onTapColor 
    switch indexPath.row { 
    case 1: 
     let firstActivityItem = 
     let secondActivityItem : NSURL = NSURL(string: "http://aspetica.sooprun.com")! 
     // If you want to put an image 

     let activityViewController : UIActivityViewController = UIActivityViewController(
      activityItems: [firstActivityItem, secondActivityItem], applicationActivities: nil) 

     activityViewController.popoverPresentationController?.sourceRect = CGRect(x: 150, y: 150, width: 0, height: 0) 

     // Anything you want to exclude 
     activityViewController.excludedActivityTypes = [ 
      UIActivityType.postToWeibo, 
      UIActivityType.print, 
      UIActivityType.assignToContact, 
      UIActivityType.saveToCameraRoll, 
      UIActivityType.addToReadingList, 
      UIActivityType.postToFlickr, 
      UIActivityType.postToVimeo, 
      UIActivityType.postToTencentWeibo 
     ] 

     self.present(activityViewController, animated: true, completion: { 
     selectedCell.backgroundColor = .clear}) 
    case 2: 
     let url = NSURL(string: "mailto:") 
     UIApplication.shared.open(url as! URL, options: [:], completionHandler: { (finish) in 
      selectedCell.backgroundColor = .clear}) 
     //   case 2: 
     //    let appID = "959379869" 
     //    if let checkURL = URL(string: "http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=\(appID)&pageNumber=0&sortOrdering=2&type=Purple+Software&mt=8") { 
     //     open(url: checkURL) 
     //    } else { 
     //     print("invalid url") 
    //    } 
    default: break 
    } 
} 

func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { 
    if indexPath.section == 0 { 
     return nil 
    } else { 
     let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath)! 
     selectedCell.backgroundColor = ColorConstants.onTapColor 
     return indexPath 
    } 
} 

所以在我didSelectRowAt我画的细胞背景颜色我想和油漆它放回功能的完成封闭,由攻上这些细胞触发。但是它并没有成为一个好用户,因为用户不知道,如果按钮被启用或者没有启动,

为了澄清,我想达到的目标, Here is what I need 和我有什么Here is what I have

+0

其实你想做什么? –

+0

我希望我的单元格在我触摸它时立即突出显示(选中),但现在只有当我将手指从其上移开时才会突出显示(触摸)。 –

+1

你会分享演示吗?如果你不介意我会检查它,并用解决方案返回给你 –

回答

1

我觉得你刚才设置你的选择风格没有。

cell.selectionStyle = UITableViewCellSelectionStyle.default 

当您选择单元格的默认选择功能的作品,找出HERE in video

您还可以看到我的视频编码。我什么都不做,只是把这行写入cellforrowat方法

+0

是的,你是对的,我已经将selectionStyle设置为.none。现在,当我将它改回原状时,它会按我的意愿工作。但是,如何更改此选择的颜色(自定义颜色)? –

+0

覆盖FUNC setHighlighted(强调:BOOL,动画:布尔)!{ 如果强调{ self.textLabel .textColor = UIColor.whiteColor() } 其他{ self.textLabel .textColor = UIColor.blackColor() } } –

+0

有了这个高亮功能,你可以根据需要设置颜色 –