我已经创建了一个显示我的数组的 tableView,它是我的表演的历史记录。 我想按月对这些表演进行“分组”,在我的数组中我得到了每场表演的创建日期。
但我对如何进行有点迷茫。这是我的表格 View 的代码。
var historic: Historic!
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.historic == nil {
return 0
}
return self.historic.performances.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("HistoricTableViewCellOne", forIndexPath: indexPath) as! HistoricTableViewCell
// Configure the cell...
if self.historic != nil {
let perf = self.historic.performances[indexPath.row]
cell.configureWithPerformance(perf)
}
return cell
}
这是我的元素
class Historic: NSObject {
var totalDistance: CLLocationDistance = 0.0
var performances: [Performance] = [Performance]()
override init() {
super.init()
}
}
class Performance: NSObject {
var urlCover: NSURL
var duration: Double = 0
var length: Double = 0
var id: UInt = 0
var publicationStatus: PerformancePublicationStatus?
var creationDate: NSDate?
init(urlCover: NSURL, creationDate: NSDate){
self.urlCover = urlCover
self.creationDate = creationDate
}
}
最终编辑: 有效!我将代码放在这里以防有人感兴趣。
var perfSections = [String]()
var tableViewCellsForSection = [Performance]()
var perfCells = Dictionary<String, [Performance]>()
// MARK: - Setup Data Source
private func setupDataSource() {
var calendar = NSCalendar.currentCalendar()
var dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale.currentLocale()
dateFormatter.timeZone = calendar.timeZone
dateFormatter.setLocalizedDateFormatFromTemplate("MMMM YYYY")
let dateComponents: NSCalendarUnit = .CalendarUnitYear | .CalendarUnitMonth
var previousYear: Int = -1
var previousMonth: Int = -1
for performance in historic.performances {
var components: NSDateComponents = calendar.components(dateComponents, fromDate: performance.creationDate!)
var year: Int = components.year
var month: Int = components.month
if (year == previousYear && month == previousMonth) {
self.tableViewCellsForSection.append(performance)
previousYear = year
previousMonth = month
}
if (year != previousYear || month != previousMonth) {
if !self.tableViewCellsForSection.isEmpty {
var sectionHeading: String = dateFormatter.stringFromDate(self.tableViewCellsForSection.last!.creationDate!)
self.perfSections.append(sectionHeading)
self.perfCells[sectionHeading] = self.tableViewCellsForSection
self.tableViewCellsForSection = []
}
self.tableViewCellsForSection.append(performance)
previousYear = year
previousMonth = month
}
}
var sectionHeading: String = dateFormatter.stringFromDate(self.tableViewCellsForSection.last!.creationDate!)
self.perfSections.append(sectionHeading)
self.perfCells[sectionHeading] = self.tableViewCellsForSection
self.tableViewCellsForSection = []
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return self.perfSections.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.historic == nil {
return 0
}
var key = self.perfSections[section]
self.tableViewCellsForSection = self.perfCells[key]!
return self.tableViewCellsForSection.count
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.perfSections[section]
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("HistoricTableViewCellOne", forIndexPath: indexPath) as! HistoricTableViewCell
// Configure the cell...
if self.historic != nil {
var key = self.perfSections[indexPath.section]
self.tableViewCellsForSection = self.perfCells[key]!
let perf = self.tableViewCellsForSection[indexPath.row]
cell.configureWithPerformance(perf)
}
return cell
}
最佳答案
问题在于您的数据模型 (self.historic.performances
) 过于简单。您需要部分和行,但您的数据模型仅表示行。您将需要一个数据模型来将您的表演分成多个部分。一种典型的方法是使用数组数组——每个外部数组是一个部分,每个内部数组是该部分的行。但当然还有许多其他方法。
关于ios - UITableView 中按月划分的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33613334/