我正在研究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
}
}
答案 0 :(得分:1)
我的应用程序在4个屏幕的列表中进行了扩展 我所设定的是
(1)我使用表视图的部分视图作为我的正常列表视图
(2)点击剖面视图上的按钮,我为该部分设置行并重新加载该部分。
(3)再次将章节按钮重置行录制为0。
=>这是实现可扩展视图的最佳和最简单的方法,无需自定义表格视图和默认动画,如卡片展开/折叠。
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和其他委托/数据源方法