【发布时间】:2017-03-31 08:08:33
【问题描述】:
我正在 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
}
}
【问题讨论】:
-
您可以轻松创建自己的实现:当用户选择任何单元格时,在数据源中所需索引处插入新数据并使用 insertRowsAtIndexPaths:withRowAnimation: 方法为插入设置动画。
-
您能否在代码方面更具体一些
标签: ios uitableview swift3 expandablelistview