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

        • <bdo id='jtmKN'></bdo><ul id='jtmKN'></ul>

        如何在swift 3中的tableview单元格内单击按钮标题

        时间:2023-07-07
      1. <small id='BILLI'></small><noframes id='BILLI'>

            <tbody id='BILLI'></tbody>

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

                  <bdo id='BILLI'></bdo><ul id='BILLI'></ul>
                  本文介绍了如何在swift 3中的tableview单元格内单击按钮标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 tableview 单元格,里面有图像视图、标签和一个 ui 按钮,我需要在点击时更改按钮标题文本和背景颜色.但是当我尝试从多个单元格中做按钮时,效果是一样的.请帮帮我!

                  I got a tableview cell inside that i have image view , label and a ui button , i need to change the button title text and background colour on click . But when i try to do button from multiple cell is having the same effect . Please do help me !

                  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                  
                      let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! subTableViewCell
                  
                      cell.catNameLabel.text = categoryNameArray[indexPath.row]
                  
                      cell.descLabel.text = descriptionArray[indexPath.row]
                  
                      cell.freqLabel.text = frequency[indexPath.row] + " at " + frequencyTime[indexPath.row]
                  
                      let url = "http://wiinnova.com/tenly/image/category_image/(categoryImageArray[indexPath.row])"
                  
                      cell.img.imageFromServerURL(urlString: url)
                      cell.button.tag = indexPath.row
                      return cell 
                  }
                  

                  推荐答案

                  首先你应该用一个 Array 来管理点击/改变 UIButton for reusableCell 否则,如果你上下滚动你的 tableView 那么它将被删除效果

                  First of all you should take one Array for manage clicked/changed UIButton for reusableCell otherwise if you scroll your tableView Up-Down then It will be remove effect

                  我正在给出我的逻辑.

                  1) 取一个可变数组名称为 temArray (大小等于您的 tableView 的行) 并且每个索引都有 0 值.你可以做到通过简单的重复值0.

                  1) Take one mutable Array name is temArray (Size equal to your tableView's Row) and it has 0 value for each index. you can do it by easy repeat value 0.

                  2) 在你的 cellForRowAtIndexPath 数据源方法检查

                  2) in you cellForRowAtIndexPath datasource method check

                  if temArray[indexPath.row] == 0 {
                     /// Write code for default UIButton - That has normal behavior means not changed title and no set background color
                  }
                  else {
                    /// Write code for What you want to keep UIButton title and background color
                  }
                  
                  cell.yourButtonObject.tag = indexPath.row
                  cell.yourButtonObject.addTarget(self, action:#selector(handleButtonClicked(_:)), for: .touchUpInside). 
                  

                  3) 并添加 handleButtonClicked 方法并更改 temArray

                  3) And add handleButtonClicked method and change temArray value

                  func handleButtonClicked(_ sender: UIButton) {
                          let myIndexPath = NSIndexPath(row: sender.tag, section: 0)
                          let cell = tblViewPref.cellForRow(at: myIndexPath as IndexPath)
                          if temArray[sender.tag] == 0 {
                          /// You can access your button by
                          // cell.myButton..... change here text and background color
                          // And change "temArray"
                  
                          temArray[sender.tag] = 1
                         }
                         else {
                  
                             /// You can access your button by
                            // cell.myButton..... change here text and background color
                            // And change "temArray"
                  
                           // SET DEFULT BUTTON TITLE AND BACKGROUND COLOR
                  
                           temArray[sender.tag] = 0
                         }
                      }
                  

                  因此,当您向上滚动表格时,temArray 将由 cellForRowAtIndexPath 数据源方法中 indexPath 处的值管理.

                  So when you scroll your table Up-down temArray will be managed by it's value at indexPath in cellForRowAtIndexPath Datasource method.

                  这篇关于如何在swift 3中的tableview单元格内单击按钮标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:子类 UITableViewCell 中的 UIButton 需要调用父类的方法 下一篇:iPhone - 如何确定在自定义 UITableViewCell 中按下按钮的单元格

                  相关文章

                    <small id='5KB4F'></small><noframes id='5KB4F'>

                        <bdo id='5KB4F'></bdo><ul id='5KB4F'></ul>

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