ios - 如何制作一个可扩展的 tableview 来显示 iOS 的其余细节?

标签 ios uitableview swift3 expandablelistview

我想利用可扩展表格 View 在用户点击单元格时显示其余表格单元格的详细信息。目前我的要求是这样的 This 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
        }

    }
}

View Controller 类代码是:

    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)")
    }

}

帮我做同样的事:

最佳答案

使用您的基础实现,我在 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 中的所有代码

希望对你有帮助

关于ios - 如何制作一个可扩展的 tableview 来显示 iOS 的其余细节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44614799/

相关文章:

ios - 取消选中 UITableview 中的单元格时,如何从数组中删除数据?

swift - CGPathAddArc 在创建路径时遇到一些问题

swift - 使用未声明的类型 'AttributedString'

ios - 如何在 swift 4.2 中为 UITableViewCell 的自定义 View 设置阴影?

ios - 如何在iPCU中设置MDM Payload的 "Identity"?

objective-c - 在iOS上按与当前位置的距离对实体进行排序的最佳策略是什么?

iphone - 禁用 uitableviewcell 上的多次点击

ios - 此应用包含一个应用扩展,该扩展指定了此版本的 iOS 不支持的扩展点标识符

ios - 在 TableCells 中重新定位时出现问题

ios - Swift 3 照片拍摄