我正在开发应用程序,我有后端API返回此数据
.... [删除]
我创建了包含5个部分的tableView,如下所示
// Number of rows each section
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
if let Sunday = DataService.DataServiceInstance.ArrayForDay("Sunday")
{
if let ArrayPerDay:NSArray = Sunday as NSArray{
return ArrayPerDay.count
}
}
}
if section == 1 {
if let Monday = DataService.DataServiceInstance.ArrayForDay("Monday")
{
if let ArrayPerDay:NSArray = Monday as NSArray{
return ArrayPerDay.count
}
}
}
if section == 2 {
if let Tuesday = DataService.DataServiceInstance.ArrayForDay("Tuesday")
{
if let ArrayPerDay:NSArray = Tuesday as NSArray{
return ArrayPerDay.count
}
}
}
if section == 3 {
if let Wednesday = DataService.DataServiceInstance.ArrayForDay("Wednesday")
{
if let ArrayPerDay:NSArray = Wednesday as NSArray{
return ArrayPerDay.count
}
}
}
if section == 4 {
if let Thursday = DataService.DataServiceInstance.ArrayForDay("Thursday")
{
if let ArrayPerDay:NSArray = Thursday as NSArray{
return ArrayPerDay.count
}
}
}
return 0
}
func numberOfSectionsInTableView(tableView:UITableView)->Int
{
return 5
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 60.0
}
然后我创建一个函数来计算每天的总行数
func ArrayForDay(dayAsk:String)->NSArray?{
var TimeTableDic = [NSDictionary]()
if let FirstArray = ArrayOfRegistredModules {
for firstArrayItem in FirstArray {
if let timetableArray:NSArray = firstArrayItem["Timetable"] as? NSArray
{
for itemInTimeTable in timetableArray{
if itemInTimeTable["Day"] as? String == dayAsk{
if let appendData = itemInTimeTable as? NSDictionary{
TimeTableDic.append(appendData)
}
}
}
}
}
}
in
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
我试过这个
if indexPath.section == 0 {
// Get Sunday Timetable
if let sunDayModel = DataService.DataServiceInstance.LoadData[0]["ModulesRegistered"] as? NSArray {
for itemInArray in sunDayModel {
for getTimeTable in itemInArray["Timetable"] as! NSArray {
if getTimeTable["Day"] as! String == "Sunday" {
print(getTimeTable)
}
//print(getTimeTable)
}
}
}
}else if indexPath.section == 1{
// Get Sunday Timetable
if let sunDayModel = DataService.DataServiceInstance.LoadData[0]["ModulesRegistered"] as? NSArray {
for itemInArray in sunDayModel {
for getTimeTable in itemInArray["Timetable"] as! NSArray {
if getTimeTable["Day"] as! String == "Monday" {
print(getTimeTable)
}
//print(getTimeTable)
}
}
}
}else if indexPath.section == 2 {
print("Tusday")
}
并且结果是重复段索引尽可能多的行,例如如果我们在0节中有3行我会得到 0 0 0
我想显示每日例子的时间表
星期日部分应该只显示星期日timeTable,我可以显示每个部分的行数,但我不知道如何为每个部分创建单元格。
谢谢。
答案 0 :(得分:0)
它很快就解析了JSON数据, 我个人建议你转换你的节数据并根据你得到的部分转换你的行数。
let moduleArray:NSArry = yourJSONDict.valueforKey("ModulesRegistered") as! NSArray
moduleArray将是您的部分数据源
现在在索引路径的地窖行
let sectionDict:NSDictionary = moduleAray[indexpath.section] as! NSDictionary
let rowArray:NSArray = sectionDict.valueforKey("Timetable") as! NSArray
现在你有你的时间表数组,你可以循环它或我个人建议NSPredict这样做..
上面的代码片段只是逻辑,你怎么能得到你想要的东西你不应该直接复制粘贴......
快乐的编码...... 祝你有个美好的一天。
答案 1 :(得分:0)
我解决了我的问题
谢谢大家。