gpt4 book ai didi

ios - UITabelView 中带有 API 数据的部分

转载 作者:行者123 更新时间:2023-11-28 06:37:57 25 4
gpt4 key购买 nike

我在 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
}

当我获取数据时,我将其解包并将其设置在事件集合中。一个事件的例子是:

注意:这是 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 月的那个只需要在 6 月出现一次,而当我在 6 月添加一个 otherwone 时,它​​需要去那里。

所以表格 View 看起来像这样:

六月

一月的细胞

6 月的单元格

一月

一月的细胞

6 月的单元格

我想要的是

六月:

6 月的单元格

一月

一月的细胞

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
}
}

所以我实际上有 2 个问题:

  1. 如何在月份数组中对数据进行排序
  2. 如何从 TableView 中的数组获取数据?

最佳答案

您的方法很好,只需稍作修改。基本上你需要按月和单独的日期数组维护一个与会者,这是如何完成的,

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

所以现在您可以使用月份数组来获取 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
}

希望对您有所帮助!

关于ios - UITabelView 中带有 API 数据的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38657583/

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