我有一个JSON数据列表:
({
"id" = 1;
"name" = "lemon tea";
"date" = 20180820;
"daycount" = 1;
})
型:
class OrderItemModal: NSObject {
var id: String!
var name: String!
var date: Date!
var daycount: String!
}
请阅读下面的 swift 文件:
(下载订单模式.swift(:
protocol OrderDownloadProtocol: class {
func itemsDownload(items: Array<Any>)
}
...
let bmsOrders = NSMutableArray()
...
weak var delegate: OrderDownloadProtocol!
let urlPath = "http://localhost/production/api/db_orders.php"
func downloadItems() {
let url = URL(string: urlPath)!
let defaultSession = Foundation.URLSession(configuration: URLSessionConfiguration.default)
...
for i in 0..<jsonResult.count
{
jsonElement = jsonResult[i] as! NSDictionary
let bmsOrder = OrderItemModal()
....
bmsOrders.add(bmsOrder)
....
(表视图控制器(
struct Objects {
var sectionName: String!
var sectionObjects: Array<Any>!
}
var sectionArray = [Objects]()
func itemsDownload(items: Array<Any>) {
orderItems = items as! [OrderItemModal]
for item in orderItems {
sectionArray += [Objects(sectionName: item. daycount, sectionObjects: item.description)]
}
}
节数:
return daysSection.count
表视图单元格:
sectionArray[indexPath.section].sectionObjects[indexPath.row] as! OrderItemModal
表视图标题:
return sectionArray[section].sectionName
行数部分:
sectionArray[section].sectionObjects.count
。 let sectionItems = groupItem[indexPath.section] let items = sectionItems[indexPath.row]
for element in items.sectionObjects {
let item = element as! OrderItemModal
cell.name?.text = item.name
所以应用程序现在可以正常运行
从外观上看,感觉您在func tableView中使用了错误的数据源(_ tableView: UITableView, cellForRowAt indexPath: IndexPath(。您正在使用包含您收到的所有数据的订单项目。
正如我从 generateDayDict(( 中了解到的那样,您刚刚隔离了您的订单项来计算您的部分。您还应该隔离您的 orderItems,以将它们作为值推送到各自的字典中(或者您希望管理数据源(,以便行选取正确的模型对象。
我会创建一个字典,其中键名作为部分,值作为 OrderItemModal 数组,它将表示 UITableView 中的各个行