سؤال

I have the following NSArray, with the array i would like to group all the objects that containing the same date. I want to group these objects and then display them in a TableView, with that group the object with the same dates in their own sections in the TableView. In each section i want the events to be listed by the time in which the event will occur starting from earliest to latest time.

The data retrieved is from a Parse Database. (Not sure if this matters).

Below is the Array Results

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}"

)

How can i group the objects that contain the same date for example "2013-11-19" all into one section?

UPDATE With Artur Answers + Slight modification to fit my needs

- (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];
هل كانت مفيدة؟

المحلول

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

This is a fast written code, so sorry for possible mistakes :)

نصائح أخرى

Here's the solution I use

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

Explanation:

  1. Parse through the list and store objects in dictionaries based on their month
  2. Store a list of those keys
  3. Reference the object:
    • by accessing the key for the section let key = monthList[indexPath.section]
    • using that value to access the right object array let objectArray = monthDictionary[key].
    • Finally, get the object by subscripting the array objectArray[indexPath.row]
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top