ios - 关闭 block 不会在第二次 swift3 上重新加载表

标签 ios swift3

我有一个带有按钮和标签的表格。当我点击按钮时,它会突出显示。所以我有 5 行,每行都有按钮和标签,当我点击每个按钮时,它们会突出显示。现在除了表格 I 的剩余 View 当我点击它时有取消按钮我希望所有选定的行再次重新加载。我的代码在第一次执行时工作正常。就像我选择所有 5 个按钮然后点击取消按钮所有行都重新加载。但是当我在表中选择按钮时再次行并点击取消没有任何反应。调用进入我的闭包函数我可以看到为重新加载打印的正确索引但没有任何反应。我的代码是这样的:

单元格自定义类-:

import UIKit

class TestingControllerCellTableViewCell: UITableViewCell {

    @IBOutlet weak var TableButton: UIButton!
    @IBOutlet weak var TableMenu: UILabel!
    var TableButtonCallBack : (()->())?
    override func awakeFromNib() {
        super.awakeFromNib()
        ButtonLayout()
        // Initialization code
    }
    func ButtonLayout()
    {
      TableButton.layer.cornerRadius = 12.5
      TableButton.layer.borderWidth = 1.0
      TableButton.layer.borderColor = UIColor.gray.cgColor
      self.selectionStyle = UITableViewCellSelectionStyle.none
    }

    @IBAction func filterTableRadioButtonAction(_ sender: UIButton) {

        TableButtonCallBack?()
    }


    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

Controller 类-:

    import UIKit

    class filterControllerViewController: UIViewController {
         @IBOutlet weak var TableViewController: UITableView!
        fileprivate var ButtonSelectedIndex = [[Int]]()
        fileprivate var cancelDataItemSelectedCallBack : ((Int)->())? = nil
        override func viewDidLoad() {
            super.viewDidLoad()
            filterTableViewSetUp()
            // Do any additional setup after loading the view.
        }

        // CANCEL ACTION
        @IBAction func cancelDataItemSelected(_ sender: UIButton) {
            for sectionIndex in 0..<filterRadioButtonSelectedIndex.count
            {
                for valueIndex in 0..<ButtonSelectedIndex[sectionIndex].count
                {
                  cancelDataItemSelectedCallBack!(ButtonSelectedIndex[sectionIndex][valueIndex])
                }

            }
            ButtonSelectedIndex.removeAll()
        }



        func TableViewSetUp()
        {
            TableViewController.delegate = self
            TableViewController.dataSource = self
            TableViewController.backgroundColor = UIColor.green
        }
            override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
        }
    }

    extension filterControllerViewController:UITableViewDataSource,UITableViewDelegate
    {
         func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
         {
           return data.count
         }

         func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
         {
            let filterCell = tableView.dequeueReusableCell(withIdentifier: "filterCell", for: indexPath) as! FilterControllerCellTableViewCell
            filterCell.filterTableRadioButtonCallBack = {
            filterCell.TableButton.backgroundColor = UIColor.blue
            self.ButtonSelectedIndex.append([indexPath.row])
            }
    // THIS cancelDataItemSelectedCallBack CALLED FIRST TIME AND RELOAD TABLE EVEN GETS CALLED SECOND TIME SHOWS CORRECT INDEX BUT TABLE BUTTONS STLL REMAIN HIGHLIGHTED

         self.cancelDataItemSelectedCallBack = { data in
                let indexPath = IndexPath(item: data, section: indexPath.section)
          print(indexPath)
                self.TableViewController.reloadRows(at: [indexPath], with: UITableViewRowAnimation.none)
            }
            return filterCell
         }

         func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
         {
          return 40.0
         }
}

我知道我遗漏了一些东西但没有得到它。请帮忙

在 self.cancelDataItemSelectedCallBac 中打印 indexPathk 给我这个正确的输出。但它只工作一次。

[0, 2]
[0, 3]

最佳答案

您必须像这样在主线程上重新加载 UI:

self.cancelDataItemSelectedCallBack = { data in

    OperationQueue.main.addOperation {
        let indexPath = IndexPath(item: data, section: indexPath.section)
        self.TableViewController.reloadRows(at: [indexPath], with: UITableViewRowAnimation.none)
    }
 }

关于ios - 关闭 block 不会在第二次 swift3 上重新加载表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43661914/

相关文章:

ios - 使用 AVFoundation 播放 wav 声音文件

iphone - 没有互联网时的 NSURLConnection 行为

ios - 如何在swift3中使用DACirularProgressView?

arrays - 向数组中插入元素时数组索引超出范围

ios - 侧滑菜单

iphone - iOS - UIButton 的文本在按下时被设置回 Nib 设置值

iphone - iOS 上的 Facebook FBConnect SDK 问题

ios - 删除扩展键盘应用程序后,仍然显示(空)-iOS键盘上的MyKeyboard键盘地球按钮

ios - 在我的选择器 View 中,数组数据未加载?

swift - 如何在 swift 3 中更改 googlemap 的 myLocationButton 的位置