Populate the section array to UITableview section

Populate the section array to UITableview section



I have a list of JSON data:


(
"id" = 1;
"name" = "lemon tea";
"date" = 20180820;
"daycount" = 1;
)



Model:


class OrderItemModal: NSObject
var id: String!
var name: String!
var date: Date!
var daycount: String!



Please read the swift file below:



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


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)]




Number of Section:


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



So the app now run okay





Can you add the code for numberOfSections, numberOfRowsInSection and how you store instances of OrderItemModal in your table view controller?
– Marco Boschi
Sep 4 '18 at 7:05



numberOfSections


numberOfRowsInSection


OrderItemModal





I have updated the code above.
– Ken
Sep 4 '18 at 9:42




1 Answer
1



From the look of it feels that you are using the wrong data source in your func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath). You are using orderItems which has all the data you received.



As I understand from generateDayDict(), you have just segregated your orderItems to calculate your sections. You should be segregating your orderItems as well to push them as values in respective dictionaries( or however you would want to manage your datasource) so that rows pick up the right model object.



I would have created a dictionary with key names as sections and values as array of OrderItemModal which will represent respective rows in UITableView





Thanks for the direction, I have updated the array set above.
– Ken
Sep 5 '18 at 4:01



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế