我想利用Expandable表视图在用户点击单元格时显示其余的表格单元格细节。目前,我的要求如下
所示的上述细胞处于扩张状态,其中下面的细胞处于未扩张状态。
目前我打算利用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)")
}
}
帮助我:
答案 0 :(得分:1)
使用您的基本实现我在ExpandingTableViewCell
static let collapsedHeigth : CGFloat = 80
static let expandedHeigth : CGFloat = 210
还在ViewController
实现
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
}
}
中的所有代码
希望这有帮助