【问题标题】:Swift. Change Color of Custom Tableview Cell Label when selected迅速。选择时更改自定义表格视图单元格标签的颜色
【发布时间】:2015-09-10 11:44:27
【问题描述】:

我在 swift 中有一个自定义 Tableview 单元格,并且在该单元格中有一个标签。

我希望能够在您选择单元格时更改标签。

如何在didSelectRowAtIndexPath 中引用我的自定义UITableviewCell 标签

在 Objective C 中,要在 didSelectRowAtIndexPath 中引用我的自定义单元格,我将使用以下内容:

MPSurveyTableViewCell *cell = (MPSurveyTableViewCell *)[tableViewcellForRowAtIndexPath:indexPath];
cell.customLabel.TextColor = [UIColor redColor]; 

我必须迅速做什么才能达到同样的效果?

【问题讨论】:

    标签: ios swift uitableview custom-cell didselectrowatindexpath


    【解决方案1】:
    let CellObject = tableView.dequeueReusableCell(withIdentifier: "your cell identifier name") as! MPSurveyTableViewCell
    CellObject.customLabel.textColor = UIColor.red
    

    【讨论】:

      【解决方案2】:

      swift 3 xcode 8

      func  tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          tableView.deselectRow(at: indexPath, animated: true)
          let Cell = tableView.cellForRow(at: indexPath)
          Cell?.textLabel?.textColor = UIColor.red // for text color
          Cell?.backgroundColor = UIColor.red // for cell background color
      }
      

      【讨论】:

      • 和单元格的背景颜色?
      • Cell?.backgroundColor = UIColor.red
      • 谢谢,几分钟后让它工作......可能是干净或摇晃的 xcode 丢失了,因为我已经有了......
      【解决方案3】:

      为什么不在 UITableViewCell 子类中使用 setSelected?

      override func setSelected(_ selected: Bool, animated: Bool) {
          super.setSelected(selected, animated: animated)
        customLabel.textColor = selected ? UIColor.red : UIColor.black
      }
      

      或者如果您希望它在特定时间后恢复为取消选择的颜色:

      override func setSelected(_ selected: Bool, animated: Bool) {
          super.setSelected(selected, animated: animated)
        if selected {
        customLabel.textColor = UIColor.red
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2, execute: { [weak self] in
          self?.customLabel.textColor = UIColor.black
          }
        }
      }
      

      然后只需设置您的单元格 selectionStyle = .none

      【讨论】:

        【解决方案4】:

        以上答案不完整

        因为 UITableView 重用单元格,您需要检查单元格是否被选中并在 cellForRowAtIndexPath 中适当调整颜色。可能有错别字,但这是完整的答案:

        func tableView(tableView: UICollectionView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
            let cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifierHere", forIndexPath: indexPath) as! MPSurveyTableViewCell 
        
            // setup your cell normally
        
            // then adjust the color for cells since they will be reused
            if cell.selected {
                cell.customLabel.textColor = UIColor.redColor()
            } else {
                // change color back to whatever it was
                cell.customLabel.textColor = UIColor.blackColor()
            }
        
            return cell
        }
        
        func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
            let cell = tableView.cellForRowAtIndexPath(indexPath) as! MPSurveyTableViewCell
            cell.customLabel.textColor = UIColor.redColor()
            tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
        
        }
        
        func tableView(tableView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
            let cell = tableView.cellForRowAtIndexPath(indexPath) as! MPSurveyTableViewCell
        
            // change color back to whatever it was
            cell.customLabel.textColor = UIColor.blackColor()
            tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
        
        }
        

        【讨论】:

        • 我尝试使用此代码作为集合视图;当我选择一个单元格时,它会被选中然后自动取消选择!所以我可以看到标签颜色改变了两次!
        • 如果您需要帮助,您必须发布代码。此答案中的代码不会取消选择单元格。如果您提出新问题,有人会帮助您。如果您在这里给我发消息或在评论中链接到它,我会尽力提供帮助。
        【解决方案5】:

        试试这个,

        func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
            tableView.deselectRowAtIndexPath(indexPath, animated: true)
        
            var Cell = tableView.cellForRowAtIndexPath(indexPath) as! MPSurveyTableViewCell
          Cell. customLabel.textColor = UIColor. redColor()
        }
        

        【讨论】:

          【解决方案6】:

          您只需要将相同的代码翻译成 Swift。

          var myCell = tableView.cellForRowAtIndexPath(indexPath) as! MPSurveyTableViewCell
          myCell.customLabel.textColor = UIColor.redColor()
          

          【讨论】:

          • 通过这个解决方案,如果我点击了很多单元格,所有单元格都会变成红色?还是只有选中的那个是红色的,其他的会变回基本颜色?
          • 我认为所有选中的单元格都会是红色的,但是如果你重复使用单元格,就会出现不一致的行为。
          • 谢谢 :) 但是您现在如何使其他单元格呈现基本颜色?真的所有被选中的单元格都是红色的,并且在选择其他单元格后没有回到基本颜色:S
          • 如果您只在 2 个单元格之间播放,那么您可以将 indexPath 保留为类变量。每次选择一个单元格时,您都会使用 savedIndexPath 获取上一个单元格,取消选择其颜色,获取更新的单元格并选择其颜色并保存新的 indexPath。
          • 不是 2 个单元格 @_@ 我正在处理 tableview,单元格数量对我来说未知:S
          猜你喜欢
          • 2012-07-10
          • 2011-04-27
          • 2012-05-26
          • 1970-01-01
          • 2015-12-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-06-25
          相关资源
          最近更新 更多