如何获取TableView

时间:2018-02-15 05:54:09

标签: ios swift uitableview swift3 tableview

我在TableView中有一个UILabel,UILabel附加到GestureRecognizer。我想要做的是在标签点击上将UILabel的颜色更改为红色。现在,当UILablel被点击时,我非常接近,Label变为红色,但错误的TableCell正在改变颜色。如果我点击单元格3,那么我希望单元格3中的UILabel改变颜色而不是单元格4或5。

class HomeProfilePlacesCell: NSObject {

    var CellCopy: HomeTVC?

    @objc func PostTap(_ sender: UIGestureRecognizer) {

         // This works but it often changes wrong cell
         CellCopy?.post.textColor = UIColor.red
    }

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

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

        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

        return cell 
    }
}

代码再次正常工作我只需要找到一种方法来将单击的索引路径单击到PostTap方法,这样它就可以改变所点击的正确单元格的颜色。任何帮助或建议都会很棒。我不想使用Table Reload,因为我只在单击的单元格中更改了1个UILabel。

5 个答案:

答案 0 :(得分:5)

无需获取单元格的索引,其中标签为Taped。 UIGestureRecognizer附加了UIView手势,因此要更改标签颜色,只需在PostTap方法中执行:

sender.view?.backgroundColor = .red

答案 1 :(得分:1)

如果要将索引传递给postTap-Method,可以继承UITapGestureRecognizer,并添加如下属性:

class CellGestureRecognizer: UITapGestureRecognizer {
     var indexPath: IndexPath?
}

现在您可以使用CellGestureRecognizer,如:

let cellGestureRecognizer = CellGestureRecognizer()
cellGestureRecognizer.indexPath = yourIndexPath

我不确定,如果这就是你想要的。希望它有所帮助。

答案 2 :(得分:1)

<强> viewDidLoad中:

for i in 0..<50 {

    favStatusDict.setValue("0", forKey: String(i))
}

cellForRowAt indexPath:

 let tapGest = UITapGestureRecognizer(target: self, action: #selector(FirstViewController.favouritesTapped))
 tapGest.numberOfTapsRequired = 1
 cell.btn.tag = indexPath.row
 cell.btn.addGestureRecognizer(tapGest)

 if favStatusDict.value(forKey:(String(indexPath.row))) as? String == "1" {

    cell.btn.backgroundColor = UIColor(red: 0/255, green: 61/255, blue: 114/255, alpha: 1.0)
 }

 else {

    cell.btn.backgroundColor = UIColor.white
 }

要更改颜色:

   func favouritesTapped(sender: UITapGestureRecognizer) {

        let tag: Int = (sender.view?.tag)!
        favStatusDict.setValue("1", forKey: String(tag))
        tableView.reloadData()
    }

答案 3 :(得分:1)

只需将selectedBackgroundView设置为cell即可。这将改变唯一选定单元格的颜色。此外,您还可以更改字体颜色和其他内容。

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

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

// MARK: Add SelectedView to TableViewCell

let selectedView:UIView = UIView(frame: CGRect(x: 0, y: 0, width: cell!.frame.size.width, height: cell!.frame.size.height))

  selectedView.backgroundColor = UIColor.red

  cell!.selectedBackgroundView = selectedView

return cell 
}

答案 4 :(得分:0)

您可以通过简单地覆盖单元格的isSelected变量来更改标签的文本颜色。在这种情况下,您无需添加点击手势,因为只要您点按任何单元格,UITableView就会自动发送触发器isSelected。以下是未向标签添加UITapGestureRecognizer的源代码。

    class HomeTVC : UITableViewCell {
       @IBOutlet weak var post: UILabel!

        @objc func postTap(_ sender: Any) {
            self.isSelected = !self.isSelected
        }

        override func awakeFromNib() {
            super.awakeFromNib()

            //Need if you want to change the color only on label tap, not on corresponding cell tap
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(postTap(_:)))
            tapGesture.delegate = self
            self.post.isUserInteractionEnabled = true
            self.post.addGestureRecognizer(tapGesture)
        }

        override var isSelected: Bool {
            willSet {
                self.post.textColor = newValue ? UIColor.red : UIColor.black
            }
        }
    }

如果您在单元格中有多个标签,并且您只需要在标签点击上更改文本的颜色,而不是在单元格点击上,那么您需要将UITapGestureRecognizer添加到标签并更改手势回调时的选定状态,如上面的代码块所述。