如何制作可扩展的tableview以显示iOS的其他细节?

问题描述 投票:1回答:1

我想利用Expandable表视图在用户点击单元格时显示其余的表格单元格细节。目前我的要求看起来像thisThis is the wireframe where i wish to use the expandable list

所示的上述电池处于膨胀状态,其中下面的电池处于未膨胀状态。

目前我计划利用Bool来检查细胞是否扩大。

请帮我解决Swift3中的情况。

目前我打算使用代码:这是自定义单元类:

import UIKit

protocol OptionButtonsDelegate{
    func closeFriendsTapped(at index:IndexPath)
}

class ExpandingTableViewCellContent {
    var title: String?
    var subtitle: String?
    var expanded: Bool

    init(title: String, subtitle: String) {
        self.title = title
        self.subtitle = subtitle
        self.expanded = false
    }
}

class ExpandingTableViewCell: UITableViewCell {

    @IBOutlet var titleLabel: UILabel!
    @IBOutlet var subtitleLabel: UILabel!
    var delegate:OptionButtonsDelegate!
    var indexPath:IndexPath!

    @IBOutlet weak var buttonClick: UIButton!
    override func awakeFromNib() {
        super.awakeFromNib()
    }

    @IBAction func buttonAction(_ sender: UIButton) {


        self.delegate?.closeFriendsTapped(at: indexPath)
    }
    func set(content: ExpandingTableViewCellContent) {
        self.titleLabel.text = content.title
        self.subtitleLabel.text = content.expanded ? content.subtitle : ""
        if content.expanded {
            self.buttonClick.isHidden = false
            self.backgroundColor = UIColor.orange
        }
        else
        {
            self.buttonClick.isHidden = true
            self.backgroundColor = UIColor.red
        }

    }
}

视图控制器类代码是:

    import UIKit

class ViewController: UIViewController,OptionButtonsDelegate {

    @IBOutlet var tableView: UITableView!
    var datasource = [ExpandingTableViewCellContent]()

    @IBOutlet weak var buttonClick: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.dataSource = self
        tableView.delegate = self
        tableView.tableFooterView = UIView() // Removes empty cell separators
        tableView.estimatedRowHeight = 60
        tableView.rowHeight = UITableViewAutomaticDimension

        datasource = [ExpandingTableViewCellContent(title: "Vestibulum id ligula porta felis euismod semper.",
                                                    subtitle: "Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Maecenas sed diam eget risus varius blandit sit amet non magna."),
                      ExpandingTableViewCellContent(title: "Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis .",
                                                    subtitle: "Etiam porta sem malesuada magna mollis euismod. Nullam quis risus urna mollis ornare vel eu leo."),
                      ExpandingTableViewCellContent(title: "Aenean lacinia bibendum nulla sed consectetur.",
                                                    subtitle: "Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec id elit non mi porta gravida at eget metus.")]
    }
}

extension ViewController : UITableViewDataSource, UITableViewDelegate {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datasource.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView .dequeueReusableCell(withIdentifier: String(describing: ExpandingTableViewCell.self), for: indexPath) as! ExpandingTableViewCell
        cell.set(content: datasource[indexPath.row])
        cell.delegate = self 
        cell.indexPath = indexPath
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let content = datasource[indexPath.row]
        content.expanded = !content.expanded
        tableView.reloadRows(at: [indexPath], with: .automatic)
    }
    func closeFriendsTapped(at index: IndexPath) {
        print("button tapped at index:\(index.row)")
    }

}

帮帮我:

ios uitableview swift3 expandablelistview
1个回答
1
投票

使用你的基本实现我在ExpandingTableViewCell中为扩展和折叠高度添加了两个静态常量

static let collapsedHeigth : CGFloat = 80
static let expandedHeigth : CGFloat = 210

还在ViewController实现中添加了heightForRowAtIndexPath方法

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
       let currentExpandingContext = datasource[indexPath.row]
        if(currentExpandingContext.expanded)
        {
            return ExpandingTableViewCell.expandedHeigth
        }else
        {
            return ExpandingTableViewCell.collapsedHeigth//fixed heigth
        }
    }

repository中的所有代码

希望这可以帮助

© www.soinside.com 2019 - 2024. All rights reserved.