我有以下NSArray,我希望将所有包含相同日期的对象分组。我想对这些对象进行分组,然后在TableView中显示它们,该组中的对象在TableView的各自部分中具有相同的日期。在每个部分中,我希望事件按照从最早到最晚的时间发生的时间列出。
检索的数据来自Parse数据库。 (不确定这是否重要)。
以下是阵列结果
DATA (
"<Sessions:QtJKSToEu9:(null)> {\n date = \"2013-11-19 18:30:00 +0000\";\n description = \"Seating assignment on name badge.\";\n location = \"Center Stage\";\n name = \"Dinner and CARE Awards Celebration\";\n}",
"<Sessions:btZ5eRZffl:(null)> {\n date = \"2013-11-19 20:30:00 +0000\";\n description = \"Brief meeting\";\n location = \"Cedar Room\";\n name = \"Store Manager Advisory Board Members\";\n}",
"<Sessions:HY4VtgBncd:(null)> {\n date = \"2013-11-20 07:00:00 +0000\";\n description = Breakfast;\n location = Ballrooms;\n name = Breakfast;\n}",
"<Sessions:6p6t2uL5Jf:(null)> {\n date = \"2013-11-20 11:30:00 +0000\";\n description = Lunch;\n location = Ballrooms;\n name = Lunch;\n}",
"<Sessions:TpEHi5Glsk:(null)> {\n date = \"2013-11-20 17:00:00 +0000\";\n description = \"Cocktails - Ballroom Pre-Function Area\";\n location = Ballroom;\n name = Cocktails;\n}",
"<Sessions:e9yEi6vFoE:(null)> {\n date = \"2013-11-20 18:00:00 +0000\";\n description = \"Welcome from Tony Kenney followed by dinner\";\n location = Ballrooms;\n name = Welcome;\n speaker = \"Tony Kenney\";\n}",
"<Sessions:kTG4NemPX4:(null)> {\n date = \"2013-11-20 19:30:00 +0000\";\n description = \"We Are Speedway Awards Night\";\n location = \"Center Stage\";\n name = \"Awards Night\";\n}",
"<Sessions:L9I8BIVECe:(null)> {\n date = \"2013-11-21 07:30:00 +0000\";\n description = Breakfast;\n location = \"Center Stage\";\n name = Breakfast;\n}",
"<Sessions:WUGRHDpHyo:(null)> {\n date = \"2013-11-21 12:00:00 +0000\";\n description = Lunch;\n location = \"Center Stage\";\n name = Lunch;\n}",
"<Sessions:r61f5NZr66:(null)> {\n date = \"2013-11-21 09:15:00 +0000\";\n description = \"Marketing Breakouts \\U2013 Rotation\\no\\tStart times for rotations: 9:15, 9:50, 10:25, 11:00, 11:35\\no\\tPlease see your name tag for your Group #2 room assignment.\\no\\tThe speaker will direct you to your next session.\\n\";\n location = Various;\n name = \"Marketing Breakouts \";\n}",
"<Sessions:RXkIDNIYoQ:(null)> {\n date = \"2013-11-20 13:30:00 +0000\";\n description = \"o\\tPlease see your name tag for your Group #1 room assignment\";\n location = Various;\n name = \"Division Breakouts\";\n}",
"<Sessions:lLI2PxdCdx:(null)> {\n date = \"2013-11-21 13:30:00 +0000\";\n description = \"Closing Comments by Tony Kenny\";\n location = \"Center Stage\";\n name = \"Closing Comments\";\n speaker = \"Tony Kenny\";\n}",
"<Sessions:KWz7Xen5a6:(null)> {\n date = \"2013-11-20 13:00:00 +0000\";\n description = \"Special Guest Speaker Gary Heminger\";\n location = \"Center Stage\";\n name = \"Special Guest Speaker\";\n speaker = \"Gary Heminger\";\n}",
"<Sessions:AU1x3LlUrF:(null)> {\n date = \"2013-11-21 08:30:00 +0000\";\n description = \"Marketing Focus by Tom LeFevers\";\n location = \"Center Stage\";\n name = \"Marketing Focus\";\n speaker = \"Tom LeFevers\";\n}",
"<Sessions:36TIRrD0s5:(null)> {\n date = \"2013-11-20 08:00:00 +0000\";\n description = \"Operations Focus by Glenn Plumby\";\n location = \"Center Stage\";\n name = \"Operations Focus\";\n speaker = \"Glenn Plumby\";\n}",
"<Sessions:tTvppNFM7L:(null)> {\n date = \"2013-11-19 17:00:00 +0000\";\n description = \"Cocktails \\U2013 Center Stage pre-function area. NOTE: Group photographs scheduled for select field personnel\\nin the lobby bar. (Appointment time on name badge.)\";\n location = \"Center Stage pre-function area \";\n name = Cocktails;\n}"
)
如何将包含相同日期的对象(例如“2013-11-19”)全部分组到一个部分?
更新使用Artur Answers +轻微修改以满足我的需求
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
int section = 0;
dataSource = [NSMutableArray new];
[dataSource addObject:[NSMutableArray array]];
for (int i = 0; i < _events.count - 1; i++) {
EventInfo *firstEvent = _events[i];
EventInfo *secondEvent = _events[i+1];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MMMM, dd"];
NSString *firstDateString = [df stringFromDate:firstEvent.details.date];
NSString *secondDateString = [df stringFromDate:secondEvent.details.date];
sectionArray = dataSource[section];
if (![sectionArray containsObject:firstEvent]) {
[sectionArray addObject:firstEvent];
}
if (![firstDateString compare:secondDateString] == NSOrderedSame) {
NSMutableArray *newSection = [NSMutableArray array];
[newSection addObject:secondEvent];
[dataSource addObject:newSection];
section++;
}
}
[dataSource.lastObject addObject:_events.lastObject];
答案 0 :(得分:5)
NSMutableArray *dataSource = [NSMutableArray new];
// Sorting your array by date asc
array = [[array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
YourObject *firstObject = obj1;
YourObject *secondObject = obj2;
return [secondObject.date compare:firstObject.date] == NSOrderedAscending;
}] mutableCopy];
int section = 0;
[dataSource addObject:[NSMutableArray array]]; //Adding first section
for (int i = 0; i < array.count - 1; i++) {
YourObject *firstObject = array[i];
YourObject *secondObject = array[i+1];
NSDate *firstDate = [self dateWithoutTimePart:firstObject.date];
NSDate *secondDate = [self dateWithoutTimePart:secondObject.date];
NSMutableArray *sectionArray = dataSource[section];
if (![sectionArray containsObject:firstObject]) {
[sectionArray addObject:firstObject];
}
if (![firstDate compare:secondDate] == NSOrderedSame)
{
NSMutableArray *newSection = [NSMutableArray array];
[newSection addObject:secondObject];
[dataSource addObject:newSection];
section++;
}
}
// Adding last object to last section
[dataSource.lastObject addObject:array.lastObject];
这是一个快速编写的代码,很抱歉可能出现错误:)
答案 1 :(得分:0)
这是我使用的解决方案
class VC: UITableViewController {
var source: DataSource!
var monthList = [String]()
var monthDictionary = [String:[object]]()
override func viewDidLoad() {
super.viewDidLoad()
let nib = UINib(nibName: "ObjectCell", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "ObjectCell")
tableView.rowHeight = 60
tableView.separatorColor = .clear
organizeObjectsIntoSections() //The magic happens here!
}
func organizeObjectsIntoSections() {
for object in source.object {
if monthDictionary[object.date.monthString] == nil { // if you the dictionary does not have a key for that month, add it as an array
monthDictionary[object.date.monthString] = [object]
} else { // otherwise append it to the existing array
monthDictionary[object.date.monthString]?.append(object)
}
if !monthList.contains(object.date.monthString) {
monthList.append(object.date.monthString)
}
}
monthList = Array(monthDictionary.keys) // store the keys for easy reference
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return monthList[section] // number of keys i.e. months in the data set
}
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
guard let header = view as? UITableViewHeaderFooterView else { return }
header.backgroundView?.backgroundColor = .white
header.textLabel?.font = UIFont(name: "Avenir-Heavy", size: 16)!
header.textLabel?.textColor = .black
}
override func numberOfSections(in tableView: UITableView) -> Int {
return monthList.count // number of months stored in the dictionary
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return monthDictionary[monthList[section]]!.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ObjectCell", for: indexPath) as? ObjectCell
else { return UITableViewCell() }
let selectedObject = objectAt(indexPath)
cell.configure(withObject: selectedObject)
return cell
}
func objectAt(_ indexPath: IndexPath) -> object {
let month = monthList[indexPath.section]
return ((monthDictionary[month]!) as [object])[indexPath.row]
}
}
说明:
let key = monthList[indexPath.section]
let objectArray = monthDictionary[key]
。objectArray[indexPath.row]