Ios 按下单元格内的按钮时如何展开单元格?
我在xcode和swift方面比较新,当整个单元格被按下时,我找到了一个扩展单元格的指南。 现在我想在按下电池内的按钮时展开电池 我的视图控制器中有:Ios 按下单元格内的按钮时如何展开单元格?,ios,swift,Ios,Swift,我在xcode和swift方面比较新,当整个单元格被按下时,我找到了一个扩展单元格的指南。 现在我想在按下电池内的按钮时展开电池 我的视图控制器中有: //datasource func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if tableView.tag == 100 { return nameArr.count }else{
//datasource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView.tag == 100 {
return nameArr.count
}else{
return prova.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView.tag == 100 {
let cell = tableView.dequeueReusableCell(withIdentifier: "MainTableViewCell") as! MainTableViewCell
cell.lblName.text = nameArr[indexPath.row]
cell.expand.tag = indexPath.row
return cell
}else{
tableView.estimatedRowHeight = 60
tableView.rowHeight = UITableView.automaticDimension
let cell = tableView.dequeueReusableCell(withIdentifier: "InsideTableViewCell") as! InsideTableViewCell
cell.lblInsideName.text = prova[indexPath.row]
return cell
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//se la cella è selezionata e deve essere tirata ancora giù ritorna 243 che sarebbe il valore della cella + l'immagine dentro da mostrare
if selectedIndex == indexPath.row && isCollapsed == true && tableView.tag == 100 {
return 375
}else {
//altrimenti se è gia collassata e ripremiamo sulla cella ritorna 50 e quindi richiude la cella
return 96
}
}
//delegate
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if selectedIndex == indexPath.row {
if self.isCollapsed == false {
self.isCollapsed = true
}else{
self.isCollapsed = false
}
}else{
self.isCollapsed = true
}
self.selectedIndex = indexPath.row
//tableView.reloadRows(at: [indexPath], with: .automatic)
tableView.beginUpdates()
tableView.endUpdates()
}
但我不知道该怎么做
在我的viewController中,现在我有一个变量:
var selectedIndex = -1 //tell me which cell i pressed
var isCollapsed = false // tell if the cell is already collapsed
更改高度后,应更新表格视图:
tableView.reloadData()
可以通过使用委托将有关该操作的信息发送到视图控制器来完成此操作
1) 您应该保存单元格的状态。可以将其存储在单元模型中:
class YourCellModel {
var isExpanded = false
}
2) 您应该创建一个委托协议:
protocol YourCellDelegate: AnyObject {
func buttonPressed()
}
3) 为单元委托和单元模型添加属性。此外,还应添加一个按下的函数按钮
:
class YourCell: UITableViewCell {
weak var delegate: YourCellDelegate?
var model: YourCellModel?
//...
@IBAction func buttonPressed() {
model?.isExpanded = true
delegate?.buttonPressed()
}
}
4) 应在视图控制器中存储单元模型:{
class YourViewController: UIViewController {
var cellModels: [YourCellModel] = []
//...
override func viewDidLoad() {
super.viewDidLoad()
cellModels = Array(repeating: YourCellModel(), count: <count of cells, maybe it is a prova.count>)
}
}
6) 更新项目的高度:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let model = cellModels[indexPath.row]
if tableView.tag == 100 {
let cell = tableView.dequeueReusableCell(withIdentifier: "MainTableViewCell") as! MainTableViewCell
cell.lblName.text = nameArr[indexPath.row]
cell.expand.tag = indexPath.row
cell.model = model
cell.delegate = self
return cell
} else {
tableView.estimatedRowHeight = 60
tableView.rowHeight = UITableView.automaticDimension
let cell = tableView.dequeueReusableCell(withIdentifier: "InsideTableViewCell") as! InsideTableViewCell
cell.lblInsideName.text = prova[indexPath.row]
cell.model = model
cell.delegate = self
return cell
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let model = cellModels[indexPath.row]
if model.isExpanded {
return 375
} else {
return 96
}
}
7) 视图控制器应实现YourCellDelegate
:
extension YourViewController: YourCellDelegate {
func buttonPressed() {
tableView.reloadData()
}
}
修改数据源以对其进行更改 当然,数据源可能更复杂,它可以是一个包含是否打开属性的对象
let nameArr:[Bool] = [false, false, false]
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.nameArr[indexPath.row] = !self.nameArr[indexPath.row]
tableView.reloadRows(at: [indexPath], with: .none)
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let isshow = self.nameArr[indexPath.row]
if isshow {
return 375
} else {
return 69
}
}
您可以在单元格中定义clouser,并在按下单元格中的按钮时调用它 在单元格中定义:
var buttonClicked: (() -> Void)?
在您的手机按钮中调用此用户
func buttonPressAction() {
buttonClicked?()
}
在cellForRow方法中,按如下方式更改:
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
如果tableView.tag==100{
将cell=tableView.dequeueReusableCell(标识符为“MainTableViewCell”)设为!MainTableViewCell
cell.buttonClicked={[weak self]在中
如果self.isCollapsed==false{
self.isCollapsed=true
}否则{
self.isCollapsed=false
}
}否则{
self.isCollapsed=true
}
}
}
但这会折叠我表格中的所有单元格view@matteoantonucci是的,你可以使用代理。我将用一个例子更新我的答案。我用视图控制器中的代码更新问题,当按下该行中的按钮时,我如何传递我必须展开的行?对不起,我不明白我必须放什么在按下func按钮的内部?@matteoantonucci我添加了一个示例。更改高度后,您应该更新表视图。是否要从单元格按钮切换您的isCollapsed
?是的,我想在按下单元格内的按钮时折叠单元格,您可以获得单元格的按钮单击cellForRowAt。选中此项。&apply lLogic用于将单元格高度增加到回调闭包中(在cellForRowAt方法中)。这是所有的表单元格还是只是特定的单元格?我必须在哪里返回我的单元格?单元格的其他内容与代码相同,我没有实现所有代码,只是显示示例,按照代码中的操作完成其余部分