【问题标题】:How can I get the correct index of a TableView如何获得 TableView 的正确索引
【发布时间】:2018-07-25 19:49:01
【问题描述】:

我在 TableView 中有一个 UILabel ,并且该 UILabel 附加到一个 GestureRecognizer 。我想要做的是在标签点击时将 UILabel 的颜色更改为红色。现在,当点击 UILablel 时,我非常接近标签变为红色,但是错误的 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。

【问题讨论】:

  • 尝试保留对单元格 (CellCopy) 的引用总是一个坏主意。不要这样做。
  • 您一次只需要更改一种标签颜色,还是需要更改每个标签标签颜色?

标签: ios swift uitableview swift3 tableview


【解决方案1】:

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

sender.view?.backgroundColor = .red

【讨论】:

  • 绝妙的答案!
【解决方案2】:

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

class CellGestureRecognizer: UITapGestureRecognizer {
     var indexPath: IndexPath?
}

现在您可以像这样使用 CellGestureRecognizer:

let cellGestureRecognizer = CellGestureRecognizer()
cellGestureRecognizer.indexPath = yourIndexPath

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      只需将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 
      }
      

      【讨论】:

        【解决方案5】:

        您可以通过简单地覆盖单元格的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 添加到标签并在手势回调时更改选定状态,如在上面的代码块中提到。

        【讨论】:

          猜你喜欢
          • 2012-07-21
          • 1970-01-01
          • 2020-05-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多