将部分数组填充到UITableview部分

时间:2018-09-04 06:41:38

标签: swift uitableview

我有一个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!
}

请阅读以下快速文件:

(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)
....

(tableview控制器)

    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

...     让sectionItems = groupItem [indexPath.section]     让items = sectionItems [indexPath.row]

for element in items.sectionObjects {
let item = element as! OrderItemModal
cell.name?.text = item.name

因此该应用现在可以正常运行

1 个答案:

答案 0 :(得分:1)

从外观上看,您在func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath)中使用了错误的数据源。您正在使用具有收到的所有数据的orderItems。

据我对generateDayDict()的了解,您刚刚隔离了orderItems以计算您的部分。您还应该分离orderItems,以将它们作为相应字典中的值推送(或者,但是您将要管理数据源),以便行选择正确的模型对象。

我会创建一个字典,其中键名作为部分,值作为OrderItemModal的数组,它将代表UITableView中的相应行