【问题标题】:How to expand tableview cell on click on button which is located outside the cell in the tableview?如何在单击位于 tableview 单元格外部的按钮时展开 tableview 单元格?
【发布时间】:2019-02-20 08:31:42
【问题描述】:

有一个表格视图单元格,并且有一个更多按钮位于表格视图上而不是单元格中,所以我希望当项目运行时它在表格视图单元格中仅显示五个用户数据,而其他用户数据显示时用户点击更多按钮(位于单元格外侧)我如何实现这一点?

感谢您的帮助

你可以找到原型细胞图像

//Mark:TableViewDelegate

extension MembersViewController:UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return imgarray.count
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tblView.dequeueReusableCell(withIdentifier: "membTableCell", for: indexPath) as! membTableCell
        cell.profiletabImg.image = imgarray[indexPath.row]
        cell.namelbl.text        =  names[indexPath.row]
        cell.positionlbl.text    =    "Ceo of code with color"
        return cell
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 40
    }
    @IBAction func morebutton(sender:UIButton){

    }


    }

【问题讨论】:

  • 贴出你试过的代码。

标签: swift


【解决方案1】:

如果向数组中添加数据并调用 tableview.reloadData() 函数,tableview 会自动展开。

来自 Apple 的文档 - “调用此方法重新加载用于构建表格的所有数据,包括单元格、节页眉和页脚、索引数组等。为了提高效率,表格视图仅重新显示那些可见的行。它会调整偏移量,如果由于重新加载,表格会缩小。当表格视图的委托或数据源希望表格视图完全重新加载其数据时,会调用此方法。不应在插入或删除行的方法中调用此方法,尤其是在动画中通过调用 beginUpdates() 和 endUpdates() 实现的块。"

    @IBAction func moreBtnPressed(_ sender: Any) {
        tableviewArray.append("Append your array with remaining items")
        tableview.reloadData()
    }

【讨论】:

  • 这是我的代码,你能给我一个例子吗
  • 在 imgarray 中只存储五个值,将剩余的值附加到更多按钮中,然后重新加载 tableview
  • 我只有一个数组,所有数据都在那个数组上,那我该怎么办
  • 最初将前五个元素复制到一个新数组中,并将剩余数据追加到更多按钮中的同一数组中
  • 如果数据来自api那我该怎么办
【解决方案2】:

如果没有点击更多按钮,则在 numberOfRowsInSection 方法中返回 5。 当点击更多按钮时,重新加载表格视图并在numberOfRowsInSection 方法中返回imgarray.count

class TableViewController: UITableViewController {

    var dataArr = [String]()
    var expanded = false

    override func viewDidLoad() {
        super.viewDidLoad()
        dataArr = (1..<25).map({ String($0) })
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "\(dataArr.count - 5) more", style: .plain, target: self, action: #selector(morebutton(_:)))
    }
    @objc func morebutton(_ sender: UIBarButtonItem) {
        self.navigationItem.rightBarButtonItem = nil
        expanded = true
        tableView.reloadData()
    }
    // MARK: - Table view data source
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if !expanded && !dataArr.isEmpty {
            return 5
        } else {
            return dataArr.count
        }
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier") ?? UITableViewCell(style: .default, reuseIdentifier: "reuseIdentifier")
        cell.textLabel?.text = dataArr[indexPath.row]
        return cell
    }
}

【讨论】:

    猜你喜欢
    • 2022-11-24
    • 1970-01-01
    • 2020-10-20
    • 1970-01-01
    • 1970-01-01
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多