首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift UITableview重新加载信元

Swift UITableview重新加载信元
EN

Stack Overflow用户
提问于 2020-12-10 13:13:29
回答 2查看 182关注 0票数 1

我坚持以下几点,任何意见都会非常感谢。

我的应用程序有一个带有自定义单元格的UITableViewController。让我们将其表视图命名为UITableView -1。我使用XIB作为它的自定义单元。在xib内部,有另一个UITableView (TABLE VIEW-2),另一个XIB作为其自定义单元格。

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

或者,执行此操作的正确方式是什么,我该如何进行?

EN

回答 2

Stack Overflow用户

发布于 2020-12-10 13:28:05

  1. 在TableView2 ViewController中创建TableView1的引用。您可以通过

执行此操作

--打开助手编辑器

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

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

然后,

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

Stack Overflow用户

发布于 2020-12-10 15:56:56

-2\f25

  • -2\f6在-2\f25 TABLE VIEW-2 \f6自定义单元格(-1\f25 TABLE VIEW-1 \f6单元格)中创建表格视图的引用-2\f25

  • -1\f6在-1\f25 TABLE VIEW-1 \f25-1\f6单元格中创建方法以重新加载其数据(可选,但对组织代码很有用)-2\f25 tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -2\f6内部调用表格视图的-1\f25 -> UITableViewCell -1\f6重新加载函数-1\f25

-1\f6。

代码语言:javascript
复制
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
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65228843

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档