我在swift 2.x和xcode 7.x中工作,我遇到了tabelviews和api数据。每一个我看起来我需要有2个数组的一个与我的对象和一个用于该部分。我目前的代码是:
//MARK: Setting up the tableview
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return eventsCollection.count
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let eventDate = self.eventsCollection[section].startDate
let month = Utility.sharedInstance.convertToDateStringMonth(eventDate)
return month
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.eventsCollection.count
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 70;
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
return
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: EventCellView!
cell = self.calenderTabelView.dequeueReusableCellWithIdentifier("EventCellView") as! EventCellView
cell.dayNameLabel.text = Utility.sharedInstance.getDayOfWeek(self.eventsCollection[indexPath.row].startDate)
cell.dayNumberLabel.text = Utility.sharedInstance.convertToDataDay(self.eventsCollection[indexPath.row].startDate)
cell.NewLabel.text = self.eventsCollection[indexPath.row].Name
return cell
}
当我获取数据时,我将其解包并在eventcollection中设置它。事件的一个例外是:
注意:这是API响应。
AllDay = 1;
Attendees = (
); <as array>
Description = "Test Evenment"; <as string>
EndDate = "2017-02-03T23:59:59+00:00"; <as string>
ID = 173; <as int or string>
Name = "Test evenement"; <string>
StartDate = "2016-11-01T00:00:00+00:00"; <string>
VenueName = "Appbakkers HQ";} <string>
当我运行代码时,它将tabelview分为以月份名称作为标题标题的部分。但我需要将事件分类到正确的月份。例如,6月份只需要在六月出现一次,当我在六月添加一个其他人时,它需要去那里。
所以tableview看起来像这样:
六月
1月的细胞
6月的细胞
一月
1月的细胞
6月的细胞
我想要的是
六月:
6月的细胞
一月
1月的细胞
the function getDayOfWeek -> monday thueseday ,etc as a string
the function convertToDataDay -> 1 - 31 as a string
the function convertToDateStringMonth -> june, juli, etc. as string
任何输入都会帮助我!感谢您的回复。
附加:
class Event: NSObject {
//the event class
var id:Int = 0
var Description: String = ""
var endDate: String = ""
var Name: String = ""
var startDate: String = ""
var venueName: String = ""
// var Attendees: NSDictionary = [:] havent figuerd this part out also
}
func mapComment(responseObject: AnyObject) -> Event{
let EventObject = Event()
// let andenteesObject = responseObject["Attendees"]
if let ID = responseObject["id"] as? Int {
EventObject.id = ID
} else if let ID = responseObject["id"] as? String {
EventObject.id = Int(ID)!
}
if let despription = responseObject["Description"] as? String{
EventObject.Description = despription
}
if let startDate = responseObject["StartDate"] as? String{
EventObject.startDate = startDate
}
if let endDate = responseObject["EndDate"] as? String{
EventObject.endDate = endDate
}
if let name = responseObject["Name"] as? String{
EventObject.Name = name
}
// if let attendees = andenteesObject as? NSDictionary{
// EventObject.Attendees = attendees
// }
return EventObject
}
}
所以我实际上有两个问题:
答案 0 :(得分:1)
您的方法很好,几乎不需要修改。基本上你需要按月保持一个与会者和单独的日期数组,这是如何做到的,
var eventsByMonth : [String : [Event]] = [:]
// store as date, its easy to sort (but anyway you like)
var months = [NSDate]()
// iterate over response and store according to month,
for event in events {
//convert the string to date (you can find it easily)
let startDate = convertStringtoDate(event.startDate)
months.append(startDate)
// now split events by months
let month = Utility.sharedInstance.convertToDateStringMonth(event.StartDate)
var temp = eventsByMonth[month] ?? []
temp.append(event)
eventsByMonth[month] = temp
}
months = sortDate(months) // sort anyway you like
所以现在你可以使用months数组来获取tableView的计数,如下所示,
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return months.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let month = months[section]
guard let monthEvents = eventsByMonth[month] else {
return 0
}
return monthEvents.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let month = months[section]
let monthEvents = events[month]!
let event = monthEvents[indexPath.row]
// configure your cell
}
希望有所帮助!