【问题标题】:Set own cell accessory type设置自己的电池配件类型
【发布时间】:2015-08-02 23:36:29
【问题描述】:

我想在 UITableViewCell 中设置我自己的 cellAccessoryType(图像)。你知道我该怎么做吗?我正在使用 Swift、Xcode 6.2 和 iOS 8.2。谢谢你的帮助!

【问题讨论】:

    标签: uitableview swift xcode6 cell accessorytype


    【解决方案1】:

    试试这个:

    // first create UIImageView
    var imageView : UIImageView
    imageView  = UIImageView(frame:CGRectMake(20, 20, 100, 320))
    imageView.image = UIImage(named:"image.jpg")
    
    // then set it as cellAccessoryType
    cell.accessoryView = imageView
    

    PS:我强烈建议你升级到 XCode 6.3.2 并使用 iOS 8.3 SDK

    【讨论】:

      【解决方案2】:

      我假设你想点击附件视图,所以我用按钮提供了这个答案。 如果没有,请使用 shargath 对 imageView 的回答。

      var saveButton = UIButton.buttonWithType(.Custom) as UIButton
              saveButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
              saveButton.addTarget(self, action: "accessoryButtonTapped:", forControlEvents: .TouchUpInside)
              saveButton.setImage(UIImage(named: "check-circle"), forState: .Normal)
              cell.accessoryView = saveButton as UIView
      
      func accessoryButtonTapped(sender:UIButton) {
      
      }
      

      【讨论】:

        【解决方案3】:

        Shmidt 答案的 Swift 5 版本,以及使用 sender.tag 来跟踪按钮被点击的行:

        override func tableView(_ tableView: UITableView, 
            cellForRowAt indexPath: IndexPath) -> UITableViewCell 
        {
            let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", 
                for: indexPath)
            let checkButton = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
            checkButton.addTarget(self, action:#selector(YOUR_CLASS_NAME.checkTapped(_:)), 
                for: .touchUpInside)
            checkButton.setImage(UIImage(named: "check"), for: .normal)
            checkButton.tag = indexPath.row
            cell.accessoryView = checkButton
            return cell
        }
        
        
        @objc func checkTapped(_ sender: UIButton) {
            print(sender.tag)
        }
        

        【讨论】:

        • 是否有可能发送“indexPath.section”和“indexPath.row”?
        • @Markus 是的,有几种不同的方法可以做到这一点,但一种简单的方法是将部分和行组合成一个数字,如 1018(第 10 部分,第 18 行)并将其分配给标签.更多信息和其他方式:stackoverflow.com/questions/32351018/…
        • 谢谢。很有意思。乍一看,我想将 2 个数字合并为 1(部分 + 行),但我不知道如何检索值,因为部分和行可以有 1 位、2 位等。我不知道创建具有额外属性的 UIButton 子类:IndexPath.
        【解决方案4】:

        Swift 5 版本,新增 contentMode:

        让 imageView: UIImageView = UIImageView(frame:CGRect(x: 0, y: 0, width: 20, height: 20)) imageView.image = UIImage(named:Imge.Card.Link.download) imageView.contentMode = .scaleAspectFit cell.accessoryView = imageView

        【讨论】:

          【解决方案5】:

          Xamarin.iOS 版 Shmidt 的回答

          // Create the accessory
          var accessory = new UIButton(UIButtonType.Custom);
          accessory.Frame = new CGRect(0f, 0f, 24f, 24f);
          accessory.AddTarget(accessoryButtonTapped, UIControlEvent.TouchUpInside);
          accessory.SetImage(UIImage.FromBundle("check-circle"), UIControlState.Normal);
          // Set the accessory to the cell
          cell.AccessoryView = accessory as UIView;
          
          void accessoryButtonTapped(object sender, EventArgs args)
          {
          
          }
          

          【讨论】:

            【解决方案6】:

            SWIFT 5 解决方案

            此示例介绍如何将带有 SF 符号的简单按钮添加到您的附件视图。

            cellForRowAt 内:

            let plusButton = UIButton(type: .system)
            plusButton.setImage(UIImage(systemName: "plus"), for: .normal)
            plusButton.contentMode = .scaleAspectFit
            plusButton.sizeToFit()
            cell.accessoryView = plusButton
            

            我们可以使用来自 UITableViewDelegate 的委托方法,而不是向按钮添加目标

            func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
                //handle logic here
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2013-04-07
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-01-24
              • 1970-01-01
              相关资源
              最近更新 更多