• <legend id='AB36c'><style id='AB36c'><dir id='AB36c'><q id='AB36c'></q></dir></style></legend>

      <small id='AB36c'></small><noframes id='AB36c'>

    1. <i id='AB36c'><tr id='AB36c'><dt id='AB36c'><q id='AB36c'><span id='AB36c'><b id='AB36c'><form id='AB36c'><ins id='AB36c'></ins><ul id='AB36c'></ul><sub id='AB36c'></sub></form><legend id='AB36c'></legend><bdo id='AB36c'><pre id='AB36c'><center id='AB36c'></center></pre></bdo></b><th id='AB36c'></th></span></q></dt></tr></i><div id='AB36c'><tfoot id='AB36c'></tfoot><dl id='AB36c'><fieldset id='AB36c'></fieldset></dl></div>

        <bdo id='AB36c'></bdo><ul id='AB36c'></ul>

        <tfoot id='AB36c'></tfoot>

        如何检测表格视图单元格中的一个按钮

        时间:2023-07-09
          <tfoot id='8nIQs'></tfoot>
            <bdo id='8nIQs'></bdo><ul id='8nIQs'></ul>
                <legend id='8nIQs'><style id='8nIQs'><dir id='8nIQs'><q id='8nIQs'></q></dir></style></legend>

                  <tbody id='8nIQs'></tbody>

                <small id='8nIQs'></small><noframes id='8nIQs'>

              • <i id='8nIQs'><tr id='8nIQs'><dt id='8nIQs'><q id='8nIQs'><span id='8nIQs'><b id='8nIQs'><form id='8nIQs'><ins id='8nIQs'></ins><ul id='8nIQs'></ul><sub id='8nIQs'></sub></form><legend id='8nIQs'></legend><bdo id='8nIQs'><pre id='8nIQs'><center id='8nIQs'></center></pre></bdo></b><th id='8nIQs'></th></span></q></dt></tr></i><div id='8nIQs'><tfoot id='8nIQs'></tfoot><dl id='8nIQs'><fieldset id='8nIQs'></fieldset></dl></div>

                1. 本文介绍了如何检测表格视图单元格中的一个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如何检测 UITableviewCell 中的一个按钮,我在 UITableViewCell 中有 10 个 UIButton,接下来当我点击 UIButton 然后它检测到多个按钮,(如奇数列表).我的 UITableView 启用了分页.这是我的所有代码.

                  How to detect one button in UITableviewCell, I have 10 UIButton in UITableViewCell, next when I click on UIButton then it detects multiple buttons, (as like odd number list). my UITableView is with paging enabled. Here is my all code.

                  表格视图

                  class HomeViewController: UIViewController, UITableViewDelegate,UITableViewDataSource {
                  
                      @IBOutlet weak var homeTableView: UITableView!
                  
                   let mainArray = [["1","2","3","4"],["5","6","7","8"],["9","10","11","12"],["13","14","15","16"]]
                  
                      override func viewDidLoad() {
                          super.viewDidLoad()
                  
                          self.homeTableView.delegate = self
                          self.homeTableView.dataSource = self
                      }
                  
                      func numberOfSections(in tableView: UITableView) -> Int {
                  
                          return mainArray.count
                      }
                  
                      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                          return mainArray[section].count
                      }
                  
                      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                  
                          let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTableViewCell", for: indexPath) as! HomeTableViewCell
                          return cell
                      }
                  
                      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
                          return self.view.frame.size.height
                      }
                  }
                  

                  TableViewCell

                  class HomeTableViewCell: UITableViewCell {
                  
                      @IBOutlet weak var bookMarkBtn: UIButton!
                  
                      @IBAction func bookMarkBtnAction(_ sender: UIButton) {
                  
                          sender.isSelected = !sender.isSelected
                  
                          if(sender.isSelected == true)
                          {
                              sender.setImage(UIImage(named:"favorite_blue"), for: UIControlState.normal)
                          }
                          else
                          {
                              sender.setImage(UIImage(named:"favorite_white"), for: UIControlState.normal)
                          }
                      }
                  }
                  

                  推荐答案

                  要检测 UITableViewCell 中的 UIButton,您可以采用以下任一方法:

                  To detect a UIButton in a UITableViewCell, you can follow any of the below approaches:

                  1.使用 UIButton IBOutlets

                  您可以创建一个 IBOutlet 对应于 UITableViewCell 中的每个 UIButton 并使用这些 outlet 来识别执行哪个按钮操作.

                  You can create an IBOutlet corresponding to each UIButton in the UITableViewCell and use those outlets to identify which button action is performed.

                  示例:

                  class CustomCell: UITableViewCell
                  {
                      @IBOutlet weak var button1: UIButton!
                      @IBOutlet weak var button2: UIButton!
                      @IBOutlet weak var button3: UIButton!
                      @IBOutlet weak var button4: UIButton!
                      @IBOutlet weak var button5: UIButton!
                  
                      @IBAction func onTapButton(_ sender: UIButton)
                      {
                          if sender === button1
                          {
                              //button1 specific code here
                          }
                          else if sender === button2
                          {
                              //button2 specific code here
                          }
                          //and so on..
                      }
                  }
                  

                  <强>2.使用 UIButton Tag 属性

                  2. Use UIButton Tag property

                  您可以为 UITableViewCell 中的每个 UIButton 提供一个 tag 值,然后使用该标签来识别特定按钮.

                  You can provide a tag value to each of the UIButton present in the UITableViewCell and then use that tag to identify the specific button.

                  示例:

                  class CustomCell: UITableViewCell
                  {
                      @IBAction func onTapButton(_ sender: UIButton)
                      {
                          if sender.tag == 1
                          {
                              //button1 has a tag = 1
                              //button1 specific code here
                          }
                          else if sender.tag == 2
                          {
                              //button2 has a tag = 2
                              //button2 specific code here
                          }
                          //and so on..
                      }
                  }
                  

                  要在 UIButton 的选中/未选中状态下设置不同的图像,您可以使用情节提要:

                  For setting different images in selected/unselected state of UIButton, you can use storyboard for that:

                  对于未选中状态:

                  对于选定状态:

                  如果您仍然遇到任何问题,请告诉我.

                  Let me know if you still face any issues.

                  这篇关于如何检测表格视图单元格中的一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么 UINavigationBar 会窃取触摸事件? 下一篇:从 UIColor 创建 UIImage 以用作 UIButton 的背景图像

                  相关文章

                  1. <legend id='DaL1L'><style id='DaL1L'><dir id='DaL1L'><q id='DaL1L'></q></dir></style></legend>
                    <i id='DaL1L'><tr id='DaL1L'><dt id='DaL1L'><q id='DaL1L'><span id='DaL1L'><b id='DaL1L'><form id='DaL1L'><ins id='DaL1L'></ins><ul id='DaL1L'></ul><sub id='DaL1L'></sub></form><legend id='DaL1L'></legend><bdo id='DaL1L'><pre id='DaL1L'><center id='DaL1L'></center></pre></bdo></b><th id='DaL1L'></th></span></q></dt></tr></i><div id='DaL1L'><tfoot id='DaL1L'></tfoot><dl id='DaL1L'><fieldset id='DaL1L'></fieldset></dl></div>
                  2. <small id='DaL1L'></small><noframes id='DaL1L'>

                      <bdo id='DaL1L'></bdo><ul id='DaL1L'></ul>
                    <tfoot id='DaL1L'></tfoot>