【问题标题】:Swift UITableview Reload CellSwift UITableview 重新加载单元格
【发布时间】:2021-03-21 12:58:27
【问题描述】:

我坚持以下,任何意见将不胜感激。

我的应用有一个带有自定义单元格的 UITableViewController。我们将其 UITableView 命名为 TABLE VIEW-1。我使用 XIB 作为其自定义单元格。在该 xib 内部,还有另一个 UITableView (TABLE VIEW-2),其中另一个 XIB 作为其自定义单元格。

我的问题是,一旦我从 (TABLE VIEW-1) 中的 API 获取数据,如何从 (TABLE VIEW-1) 重新加载 (TABLE VIEW-2) 的单元格。我想使用委托和协议来做到这一点。

或者,执行此操作的正确方法是什么?我将如何去做?

【问题讨论】:

    标签: swift uitableview


    【解决方案1】:
    1. 在 TABLE VIEW-1 自定义单元格 (TABLE VIEW-1 CELL) 中创建 TABLE VIEW-2 的引用
    2. 在 TABLE VIEW-1 CELL 中创建一个方法,以便使用 TABLE VIEW-2 单独重新加载其数据(可选,但对组织代码很有用)
    3. tableView内部(_tableView: UITableView, cellForRowAt indexPath: IndexPath) -> TABLE VIEW-1调用reload函数的UITableViewCell
    class ViewController: UIViewController {
        
        @IBOutlet weak var mainTable: UITableView!
        
        let sampleData = [
            (section: "News", SubSections: [
                "Breaking news",
                "Current Affairs",
                "Local"
            ]) ]
    
        override func viewDidLoad() {
            super.viewDidLoad()
            
            self.mainTable.estimatedRowHeight = 200
            self.mainTable.rowHeight = UITableView.automaticDimension
            
            let nib: UINib = UINib(nibName: String(describing: MainCell.self), bundle: Bundle.main)
            self.mainTable.register(nib, forCellReuseIdentifier: String(describing: MainCell.self))
        }
    }
    
    extension ViewController: UITableViewDataSource {
        
        func numberOfSections(in tableView: UITableView) -> Int {
            1
        }
        
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            self.sampleData.count
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            
            let cell: MainCell = tableView.dequeueReusableCell(withIdentifier: String(describing: MainCell.self), for: indexPath) as! MainCell
            
            let sectionData = self.sampleData[indexPath.row]
            
            cell.setUpData(title: sectionData.section, data: sectionData.SubSections)
            
            return cell
        }
    }
    
    
    class MainCell: UITableViewCell {
        
        @IBOutlet weak var sectionTitle: UILabel!
        @IBOutlet weak var internalTable: UITableView!
        
        var tableData:[String]?
    
        override func awakeFromNib() {
            super.awakeFromNib()
            
            self.internalTable.estimatedRowHeight = 200
            self.internalTable.rowHeight = UITableView.automaticDimension
            
            let nib: UINib = UINib(nibName: String(describing: InsideCell.self), bundle: Bundle.main)
            self.internalTable.register(nib, forCellReuseIdentifier: String(describing: InsideCell.self))
        }
    
        func setUpData(title: String, data:[String]) {
            self.sectionTitle.text = title
            self.tableData = data
            self.internalTable.reloadData()
        }
        
    }
    
    extension MainCell: UITableViewDataSource {
        
        func numberOfSections(in tableView: UITableView) -> Int {
            1
        }
        
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            self.tableData?.count ?? 0
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            
            let cell: InsideCell = tableView.dequeueReusableCell(withIdentifier: String(describing: InsideCell.self), for: indexPath) as! InsideCell
            
            if let subSectionData = self.tableData?[indexPath.row] {
                cell.subSectionTitle.text = subSectionData
            }
            
            return cell
        }
    }
    
    class InsideCell: UITableViewCell {
        
        @IBOutlet weak var subSectionTitle: UILabel!
    
        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
        }
    
        override func setSelected(_ selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)
    
            // Configure the view for the selected state
        }
    }
    
    

    【讨论】:

    • 感谢 Niroshan 的回复。我会努力回来的。
    • 这会重新加载整个表格还是只加载一个单元格?
    【解决方案2】:
    1. 在 TableView2 ViewController 中创建 TableView1 的引用。您可以这样做

    -- 打开助手编辑器

    -- 右键单击​​并从您的 UI 元素(即标签)拖动到代码文件

    -- Xcode 自动插入代码为您创建名称和连接

    1. 然后调用tableView2.reloadData(),其中tableView2 是您刚刚创建的插座的名称。

    【讨论】:

    • 感谢您的回复。这两个表有不同的类。 Xcode 无法在 TableView2 ViewController 中创建 TableView1 的引用。
    猜你喜欢
    • 2011-07-27
    • 1970-01-01
    • 2011-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多