gpt4 book ai didi

ios - 如何设置可展开和可折叠的tableview单元格在加载时可展开

转载 作者:行者123 更新时间:2023-11-29 00:25:30 26 4
gpt4 key购买 nike

我正在 swift 3.0 中开发一个项目,我想将 UITabeViewCells 设置为在第一次加载时展开。到目前为止,我已经成功创建了 UITableViewCells 以在选择一行后展开。代码如下。

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

var destinationData: [HeaderData?]?

@IBOutlet weak var expandableTableView: UITableView!

override func viewDidLoad() {
super.viewDidLoad()

destinationData = getData()

}
private func getData() -> [HeaderData?] {
// let data: [HeaderData?] = []

let freeGiftsList = [ContainerData(mp3Title: "MP3Title", category: "Category",teaserDescription:"TeaserDescription")]
let freeGifts = HeaderData(name: "Free Gifts", imageName: "header1", expcell: freeGiftsList)

let exclusiveOffersList = [ContainerData(mp3Title: "MP3Title", category: "Category",teaserDescription:"TeaserDescription")]
let exclusiveOffers = HeaderData(name: "Exclusive Offers", imageName: "header2", expcell: exclusiveOffersList)

let allAudiosAndPackagesList = [ContainerData(mp3Title: "MP3Title", category: "Category",teaserDescription:"TeaserDescription")]
let allAudiosAndPackages = HeaderData(name: "All Audios & Packages", imageName: "header3", expcell: allAudiosAndPackagesList)

return [freeGifts, exclusiveOffers, allAudiosAndPackages]
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()



}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let data = destinationData {
return data.count
} else {
return 0
}

}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let rowData = destinationData?[indexPath.row] {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! HeaderTableViewCell
headerCell.headerNameLabel.text = rowData.name
headerCell.backgroundView = UIImageView(image: UIImage(named: rowData.imageName))
headerCell.selectionStyle = .none
return headerCell
}
// Row is ExpansionCell
else {
if let rowData = destinationData?[getParentCellIndex(expansionIndex: indexPath.row)] {
// Create an ExpansionCell
let expansionCell = tableView.dequeueReusableCell(withIdentifier: "ExpCell", for: indexPath) as! ExpandableTableViewCell

// Get the index of the parent Cell (containing the data)
let parentCellIndex = getParentCellIndex(expansionIndex: indexPath.row)

// Get the index of the flight data (e.g. if there are multiple ExpansionCells
let expIndex = indexPath.row - parentCellIndex - 1

// Set the cell's data
expansionCell.categoryLabel.text = rowData.expOffers?[expIndex].category
expansionCell.mp3TitleLabel.text = rowData.expOffers?[expIndex].mp3Title
expansionCell.teaserDescription.text = rowData.expOffers?[expIndex].teaserDescription

expansionCell.selectionStyle = .none
return expansionCell
}
}
return UITableViewCell()

}


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if (destinationData?[indexPath.row]) != nil {

// If user clicked last cell, do not try to access cell+1 (out of range)
if(indexPath.row + 1 >= (destinationData?.count)!) {
expandCell(tableView: tableView, index: indexPath.row)
}
else {
// If next cell is not nil, then cell is not expanded
if(destinationData?[indexPath.row+1] != nil) {
expandCell(tableView: tableView, index: indexPath.row)
// Close Cell (remove ExpansionCells)
} else {
contractCell(tableView: tableView, index: indexPath.row)

}
}
}
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if (destinationData?[indexPath.row]) != nil {
return 40
} else {
return 100
}
}



/* Expand cell at given index */
private func expandCell(tableView: UITableView, index: Int) {
// Expand Cell (add ExpansionCells
if let flights = destinationData?[index]?.expOffers {
for i in 1...flights.count {
destinationData?.insert(nil, at: index + i)
tableView.insertRows(at: [NSIndexPath(row: index + i, section: 0) as IndexPath] , with: .top)
}
}
}

/* Contract cell at given index */
private func contractCell(tableView: UITableView, index: Int) {
if let flights = destinationData?[index]?.expOffers {
for _ in 1...flights.count {
destinationData?.remove(at: index+1)
tableView.deleteRows(at: [NSIndexPath(row: index+1, section: 0) as IndexPath], with: .top)

}
}
}

/* Get parent cell index for selected ExpansionCell */
private func getParentCellIndex(expansionIndex: Int) -> Int {

var selectedCell: HeaderData?
var selectedCellIndex = expansionIndex

while(selectedCell == nil && selectedCellIndex >= 0) {
selectedCellIndex -= 1
selectedCell = destinationData?[selectedCellIndex]
}

return selectedCellIndex
}



}

最佳答案

我的应用程序在列表中扩展了 4 个屏幕我设置的是

(1) 我使用表格 View 的部分 View 作为我的普通 ListView

(2) 在点击剖面 View 上的按钮时,我为该部分设置了行并重新加载该部分。

(3) 再次点击部分按钮将行重置为 0。

=> 这是实现可展开 View 的最好、最简单的方法,无需自定义表格 View 和卡片展开/折叠等默认动画。

    func numberOfSections(in tableView: UITableView) -> Int {
return self.arrMainList.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (self.arrIndexPathSelectedRow.contains(section)) {
let objItem = self.arrExpandedList[section] as! myObj
let myJournals = JournalOperationModel.sharedInstance.getGroupedJournalList(objItem)
return myJournals.count
}
else{
return 0
}
}

我有另一个名为“arrIndexPathSelectedRow”的数组,将用于确定展开或折叠卡片。

并实现 IU 和其他委托(delegate)/数据源方法

这是我用过的方法 enter image description here

关于ios - 如何设置可展开和可折叠的tableview单元格在加载时可展开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43135645/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com