【问题标题】:Swift Change label text color on tap from within TableViewCell从 UITableViewCell 中点击 Swift 更改标签文本颜色
【发布时间】:2018-01-30 05:01:19
【问题描述】:

我有一个UILabelTableView 内,我想在用户点击时将 UILabel 的颜色更改为红色。我正在使用UITapGestureRecognizer,在点击UILabel 时,我可以获得UILabel 的内容,但我无法获得实际的UILabel,因为据我所知,UIGesture 中不能有参数功能。

这是我的代码,它可以帮助你搞清楚

class HomeProfilePlacesCell: NSObject {

    var Post =  [String]()

    @objc func PostTap(_ sender: UIGestureRecognizer) {
        print(Post[(sender.view?.tag)!])
    }

    func HomeProfilePlaceTVC(_ tableView: UITableView, cellForRowAt indexPath: IndexPath, streamsModel : streamModel,HOMEPROFILE: HomeProfile, controller: UIViewController) -> UITableViewCell {

         let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTVC", for: indexPath) as! HomeTVC

         let tapGesture = UITapGestureRecognizer(target: self, action: #selector(PostTap(_:)))
         tapGesture.delegate = self as? UIGestureRecognizerDelegate
         cell.post.addGestureRecognizer(tapGesture)

         cell.post.text = streamsModel.Posts[indexPath.row]
         cell.post.tag = indexPath.row
         Post = streamsModel.Posts

         return cell 
    }
}

我的功能是 PostTap 每当用户点击UILabel 这是 cell.post 然后我可以在 PostTap 中阅读它的内容> 但是为了改变 UILabel 的颜色,我必须将 let cell 常量传递给 PostTap 函数。

无论如何我可以做到这一点或解决方法吗?我是 Swift 新手

【问题讨论】:

  • 在你的情况下:call.delegate = controller
  • 试试这个@objc func PostTap(_ sender: UIGestureRecognizer) { sender.view?.backgroundColor = .red }

标签: ios swift swift3 uigesturerecognizer


【解决方案1】:

使用 TableView 代理:[SWIFT 4.0]

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
    let cell = tableView.cellForRowAtIndexPath(indexPath) as! <your Custom Cell>
    cell.<your CustomCell label name>.textColor = UIColor.red
    //OR
    cell.<your Customcell label name>.backgroundColor = UIColor.green
    tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
}

func tableView(tableView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) 
{
    let cell = tableView.cellForRowAtIndexPath(indexPath) as! <your Custom Cell>

    // change color back to whatever it was
    cell.<your Customcell label name>.textColor = UIColor.black
    //OR
    cell.<your Customcell label name>.backgroundColor = UIColor.white
    tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
}

【讨论】:

    【解决方案2】:

    将标签添加到单元格为 indexPath.row

    cell.tag = indexPath.row
    

    然后

    @objc func PostTap(_ sender: UIGestureRecognizer) {
         let cell = self.tableVIew.cellForRow(at: sender.tag) as! HomeTVC
          // Now you access your cell label here, and can do whatever you want
    
    }
    

    【讨论】:

    • 这很脆弱,如果 tableview 允许行重新排序(或行插入/删除)会破坏
    • 这个方法会在你点击它时被调用。行重新排序不会破坏它
    • 假设屏幕上有 5 个单元格,它们的标签是 0-4。现在我删除第 3 行的单元格,从而显示一个新单元格来填补空白。我现在在屏幕上有带有以下标签的单元格; 0,1,2,4,5,5。您可以专门重置 didMove... 方法中的标签来处理我猜的这种情况。
    • 行删除后,您必须重新加载表视图。重新加载tableview时,再次调用cell for row,标签会自动重置。
    • 但这会导致视觉体验不佳,并且在重新加载整个 tableview 时会进行不必要的工作。相信我,总有比使用标签更好的解决方案。例如,如果您的 tableview 有部分怎么办?标签以多种方式中断。
    【解决方案3】:

    你可以通过使用来实现它

     tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath)
    

    当用户点击单元格时,此方法调用 在这个方法中这样做

    tableView.cellForRow(at: indexPath)
    

    这将使您将单元格转换为您的单元格类 现在你可以在那个单元格中用你的标签做任何事情 单元格.标签....

    【讨论】:

      【解决方案4】:

      要更改点击索引标签的颜色,首先需要在变量上声明以识别点击位置

      var selectedCellIndex = "" // initialize as empty string
      

      在你的 cellForRowAt

       func HomeProfilePlaceTVC(_ tableView: UITableView, cellForRowAt indexPath: IndexPath, streamsModel : streamModel,HOMEPROFILE: HomeProfile, controller: UIViewController) -> UITableViewCell {
      
               let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTVC", for: indexPath) as! HomeTVC
      
      
      
               cell.post.text = streamsModel.Posts[indexPath.row]
               cell.post.tag = indexPath.row
               cell.post.isUserInteractionEnabled = true
      
                let tapGesture = UITapGestureRecognizer(target: self, action: #selector(PostTap(_:)))
               tapGesture.delegate = self as? UIGestureRecognizerDelegate
               cell.post.addGestureRecognizer(tapGesture)
               Post = streamsModel.Posts
      
               if self.selectedCellIndex == "\(indexPath.row)" {
                  cell.post.text = UIColor.red
               } else {
                  cell.post.text = UIColor.blue
               }
      
               return cell 
          }
      

      在您的点按功能中

      func PostTap(_ sender:UIGestureRecognizer){
          let tapView = gesture.view!
          let index = tapView.tag
          self. selectedCellIndex = "\(index)"
          self.YOUR_TABLE_NAME.reloadData()
       }
      

      希望对你有帮助

      【讨论】:

        【解决方案5】:

        在 Cell 中尝试Closure 方法

        在自定义表格视图单元格中:

        class HomeTVC: UITableViewCell {
        
            @IBOutlet weak var labelPost: UILabel!
        
            var callBackOnLabelTap: (()->())?
        
            override func awakeFromNib() {
                super.awakeFromNib()
                // Initialization code
        
                let tapGesture = UITapGestureRecognizer(target: self, action: #selector(postTap(_:)))
                tapGesture.numberOfTapsRequired = 1
                tapGesture.delegate = self
                self.labelPost.addGestureRecognizer(tapGesture)
            }
        
            @objc func postTap(_ sender: UIGestureRecognizer) {
                self.callBackOnLabelTap?()
            }
        
            override func setSelected(_ selected: Bool, animated: Bool) {
                super.setSelected(selected, animated: animated)
        
                // Configure the view for the selected state
            }
        }
        

        然后在cellForRowAt indexPath

        func HomeProfilePlaceTVC(_ tableView: UITableView, cellForRowAt indexPath: IndexPath, streamsModel : streamModel,HOMEPROFILE: HomeProfile, controller: UIViewController) -> UITableViewCell {
        
             let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTVC", for: indexPath) as! HomeTVC
        
             cell.callBackOnLabelTap = {
                cell.labelPost.backgroundColor = UIColor.black
            }
        
             return cell 
        }
        

        【讨论】:

          【解决方案6】:

          对我来说,我希望在点击标签的容器单元格时改变标签的颜色。 您可以为标签文本选择您想要的颜色,点击时选择标签的突出显示(在属性检查器中)。从下拉列表中,您可以选择点击单元格时想要看到的颜色。

          属性检查器:标签的突出显示属性

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-12-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-02-09
            • 2020-02-04
            • 2016-12-26
            • 2015-02-02
            相关资源
            最近更新 更多