从TableViewCell中点击Swift Change标签文本颜色

时间:2018-01-30 05:01:19

标签: ios swift swift3 uigesturerecognizer

我有一个Event.findAll({ raw : true, attributes : [ 'Event.*' , 'EventDates.eventId' , [sequelize.fn('max', sequelize.col('EventDates.end')),'end']], include : { model : EventDates , attributes :[] }, group : ['Event.id','EventDates.eventId'] }) 位于Event.findAll({ raw : true, attributes : [ 'Event.*' , 'dates.*' , 'EventDates.eventId' , [sequelize.fn('max', sequelize.col('EventDates.end')),'end']], include : [ { model : EventDates , attributes :[] }, { model : EventDates , as : 'dates', required : false, separate : true } ] group : ['Event.id','EventDates.eventId'] }) 内,我想在用户点按时将UILabel的颜色更改为红色。我正在使用UILabel并点击TableView我可以获取UITapGestureRecognizer的内容,但我无法获得实际UILabel因为据我所知,你可以' t在UILabel函数中有参数。

这是我的代码,它将有助于清理

UILabel

我的功能是 PostTap 每当用户点击UIGesture cell.post 时,我就可以在 PostTap <中读取它的内容/ strong>但是为了改变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 } } 的颜色,我必须将 let cell 常量传递给 PostTap 函数。

无论如何,我能做到这一点还是可以解决一下?我是Swift的新手

6 个答案:

答案 0 :(得分:2)

使用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)
}

答案 1 :(得分:1)

你可以使用

来实现
 tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath)

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

tableView.cellForRow(at: indexPath)

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

答案 2 :(得分:1)

将标记添加到单元格作为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

}

答案 3 :(得分:1)

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

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

希望这会对你有所帮助

答案 4 :(得分:1)

在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 
}

答案 5 :(得分:0)

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

  

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