У меня есть список данных 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 ниже:
(DownloadOrderModal.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
TableViewCell:
sectionArray[indexPath.section].sectionObjects[indexPath.row] as! OrderItemModal
TableViewTitle:
return sectionArray[section].sectionName
numberOfRowsInSection:
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
Так что теперь приложение работает нормально