【问题标题】:tableview sections from json来自json的tableview部分
【发布时间】:2018-08-05 16:39:38
【问题描述】:

我正在尝试使用来自 JSON 服务器响应的部分填充 tableview 中的日期...

这些部分将是日期.. 我想将具有相同日期的对象添加到同一部分...

我正在尝试这样做:

                print("heree:")
                print(matches.data?.count)
                for matchdate in matches.data!{
                    datesarray.append(matchdate.dateGregorian!)
                }
                datesarray = datesarray.reduce([], {
                    $0.contains($1) ? $0 : $0 + [$1]
                })
                print(datesarray.count)
                for date in datesarray{
                    for match in matches.data!{
                        if date == match.dateGregorian{
                            homeimg = clubImg(id: match.home!)
                            awayimg = clubImg(id: match.away!)
                            matchesarray.append(Matches2(homeimg: homeimg,homenamear: match.homeClubNameAr,awayimg: awayimg,awaynamear: match.awayClubNameAr,matchtime: match.dateHijri))
                        }
                        self.objectArray.append(Sections(sectionDate: date, sectionMatches: matchesarray))
                    }
                }
                DispatchQueue.main.async {
                    self.tableview.reloadData()
                }
                print("heree2:")
                print(self.objectArray.count)

但我得到了一个非常错误的结果!

json 正在返回一个包含 149 个对象的数组 .. uniq 数组有 74 个 .. 最后 tableview 数组有 11026 个对象!!!!!!如何以及为什么?

做错了什么以及如何解决?

【问题讨论】:

  • 你把这段代码放在哪里了?在 cellForRowAtIndexPath 方法中?
  • @Hoven no 在 vi​​ewdidload() 中调用的方法中

标签: ios swift xcode


【解决方案1】:

你可以试试

for date in datesarray{
  matchesarray.removeAll()
  for match in matches.data!{
     if date == match.dateGregorian{
         homeimg = clubImg(id: match.home!)
         awayimg = clubImg(id: match.away!)
         matchesarray.append(Matches2(homeimg: homeimg,homenamear: match.homeClubNameAr,awayimg: awayimg,awaynamear: match.awayClubNameAr,matchtime: match.dateHijri))
     }      
  }
  self.objectArray.append(Sections(sectionDate: date, sectionMatches: matchesarray))
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-18
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 1970-01-01
    相关资源
    最近更新 更多