简体   繁体   中英

iPhone UITableView populating variable rows sections from flat array

I thought that would be very common and easy iPhone App. In the main app I connect to database, retrieve values from database (NSDate converted to NSString)n and put into single array. Then in one of the views I populate UITableView with elements from the array. UITableView is grouped (sections). I step through array to discover number of sections (change section if new day). How do I retrieve correct element of array in cellForRowAtIndexPath? IndexPath.section and IndexPath.row seem useless as row starts count from zero for each section. If number of rows in each section was the same it would have been easy:

[arryData objectAtIndex:(indexPath.row)+indexPath.section*[tblMatchesView numberOfRowsInSection:indexPath.section]];

But number of rows in each section varies... :-)

从数据库中获取数据后,将数据分成多个数组(基于不同的天数),这将是最简单的解决方案...

It doesn't have absolute cursor but you can try utilizing UILocalizedIndexedCollation class which is used to ease the "sectioning" of your data and proving the tableView delegate functions the required data such as the index titles, etc

Here's apple documentation link for it: https://developer.apple.com/library/ios/#documentation/iPhone/Reference/UILocalizedIndexedCollation_Class/UILocalizedIndexedCollation.html#//apple_ref/occ/cl/UILocalizedIndexedCollation

How about storing different date sections in different arrays? For example, you can have an array A of array. You can loop through the original arrays yourself, if you found a new day, you just create a new array and put it into the array A. And then, when you loop over the cell, you can get the section number to get the correct array and based on the row number to get the correct elemenet in the array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM