UA-17470720-3

Jump to content


Photo
- - - - -

Master View, concatenate Sections by Year

sections

  • Please log in to reply
17 replies to this topic

#1 David DelMonte

David DelMonte

    Forum Master

  • STV 4.0 Pro
  • PipPipPipPipPipPipPip
  • 527 posts
Reputation: 41
Excellent

Posted 18 December 2014 - 03:24 PM

I have data that is ordered by year. A year can have several records.

 

What's the best way, in STV 4 to concatenate the records so that it looks something like this:

 

 

1990

Task 1

Task 2

Task 3

 

1992

Task 1

Task 2

 

1995

Task 1

 

etc....

 

It would be even greater if the year could be a section header.



#2 ozie

ozie

    ¯\_(ツ)_/¯

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 526 posts
  • LocationAustralia
Reputation: 169
Popular

Posted 18 December 2014 - 04:54 PM

that would be part of the SCDataFetchOptions class

 

.dataFetchOptions.sortKey = @"title";
.dataFetchOptions.sortAscending = yes or no 

 

can then use sectionHeaderTitleForItem to set the title


P.S. I hate Swift.. don't talk Swift.. Too old school to learn yet another programming language.


#3 David DelMonte

David DelMonte

    Forum Master

  • STV 4.0 Pro
  • PipPipPipPipPipPipPip
  • 527 posts
Reputation: 41
Excellent

Posted 18 December 2014 - 08:56 PM

Ozie / Tarek, could either of you write a code snip that would a: Create sections in a Master View and b: how to use expand/contract cells also in a master view.

 

Many thanks!



#4 ozie

ozie

    ¯\_(ツ)_/¯

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 526 posts
  • LocationAustralia
Reputation: 169
Popular

Posted 18 December 2014 - 09:17 PM

what is your main end game your looking for.. as per your first post might imply.. just to know what sort of code to write

 

do you want section headings and then a list of tasks within each section, and each task cells are then expand/collapse able??

or something where the sections themselves expand and collapse to show the tasks??


P.S. I hate Swift.. don't talk Swift.. Too old school to learn yet another programming language.


#5 David DelMonte

David DelMonte

    Forum Master

  • STV 4.0 Pro
  • PipPipPipPipPipPipPip
  • 527 posts
Reputation: 41
Excellent

Posted 19 December 2014 - 03:38 AM

Thanks Ozie, good question and I wasn't so clear. So, like my first post, but with the year as the collapsable cell. So I can see a list of years, and expand any to see the tasks in each.



#6 ozie

ozie

    ¯\_(ツ)_/¯

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 526 posts
  • LocationAustralia
Reputation: 169
Popular

Posted 19 December 2014 - 06:48 PM

have fun

you will need to add the STV frameworks to the project

 

Attached File  CollapseHeaders.zip   49.41K   8 downloads


  • David DelMonte likes this

P.S. I hate Swift.. don't talk Swift.. Too old school to learn yet another programming language.


#7 David DelMonte

David DelMonte

    Forum Master

  • STV 4.0 Pro
  • PipPipPipPipPipPipPip
  • 527 posts
Reputation: 41
Excellent

Posted 19 December 2014 - 07:26 PM

Thanks Ozie.. Just what I needed... I really appreciate the help..



#8 David DelMonte

David DelMonte

    Forum Master

  • STV 4.0 Pro
  • PipPipPipPipPipPipPip
  • 527 posts
Reputation: 41
Excellent

Posted 19 December 2014 - 07:43 PM

Blast, spoke too soon... All's well until I add something, then on saving, I get  "attempt to insert row 1 into section 21, but there are only 1 rows in section 21 after the update"

 

- Is that me?

 

It works OK if the sections are open :

 

section.expandCollapseCell = [SCExpandCollapseCell cellWithExpandText:section.headerTitle collapseText:section.headerTitle ownerSectionExpanded:TRUE];


Edited by David DelMonte, 19 December 2014 - 07:46 PM.


#9 ozie

ozie

    ¯\_(ツ)_/¯

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 526 posts
  • LocationAustralia
Reputation: 169
Popular

Posted 19 December 2014 - 07:55 PM

this will need modification in the source code to fix this

 

@Tarek

1- we need a bug report section for STV 4.0.. currently there is only a section for 3.0 :)

 

2- when the section uses an SCExpandCollapseCell and the section is not expanded, creating a new item will crash the app

    gonna have to add some code with the function below to test if the section is controlled by expand/collapse cell and test for collapsed and stop the tableview insert code from running

- (void)addCellForNewItem:(NSObject *)newItem

 

 

Oz


Edited by ozie, 19 December 2014 - 08:21 PM.

P.S. I hate Swift.. don't talk Swift.. Too old school to learn yet another programming language.


#10 David DelMonte

David DelMonte

    Forum Master

  • STV 4.0 Pro
  • PipPipPipPipPipPipPip
  • 527 posts
Reputation: 41
Excellent

Posted 19 December 2014 - 10:39 PM

i think, add a second. Using Ozie's code, add a few cells, and open/close each section. The graphics get out of whack a bit



#11 David DelMonte

David DelMonte

    Forum Master

  • STV 4.0 Pro
  • PipPipPipPipPipPipPip
  • 527 posts
Reputation: 41
Excellent

Posted 20 December 2014 - 05:24 AM

@all.. In my top post above, I show a data hierarchy like this:

 

1990

Task 1

Task 2

Task 3

 

1991

Task 1

Task 2

 

I want to put counters for each section, so that this would look like this:

 

1990 (3)

Task 1

Task 2

Task 3

 

1991 (2)

Task 1

Task 2

 
I've tried this
 

 

model.modelActions.sectionHeaderTitleForItem = ^NSString*(SCArrayOfItemsModel *itemsModel, NSObject *item, NSUInteger itemIndex){
NSString *year = [item valueForKey:@"year"];
NSString *yearHeader = [NSString stringWithFormat:@"%@ (%ld)", year, (unsigned long)model.sectionCount ];
return yearHeader;
};
 

 

 

However, that just gives me the sequence number of each section. I'll keep looking but help is a time saver :)


Edited by David DelMonte, 20 December 2014 - 09:25 AM.


#12 ozie

ozie

    ¯\_(ツ)_/¯

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 526 posts
  • LocationAustralia
Reputation: 169
Popular

Posted 20 December 2014 - 03:02 PM

what about itemsModel.items

 

and do a search on here for SCBadgeView.. you can use it like cell.badgeview etc.. might be more what you want to display


P.S. I hate Swift.. don't talk Swift.. Too old school to learn yet another programming language.


#13 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 22 December 2014 - 10:24 AM

Hi David,

 

You last code sample with "sectionHeaderTitleForItem" is exactly the way to go. What do you mean by "that just gives me the sequence number of each section"?



#14 David DelMonte

David DelMonte

    Forum Master

  • STV 4.0 Pro
  • PipPipPipPipPipPipPip
  • 527 posts
Reputation: 41
Excellent

Posted 22 December 2014 - 10:43 AM

Hi Tarek, with the code snip above, I get this result:

 

Attached File  Screen Shot 2014-12-22 at 1.41.05 PM.png   278.67K   20 downloads

 

 



#15 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 22 December 2014 - 11:10 AM

That's exactly what your code describes. Your code is returning a different string for every single item, so you will get a separate section for every single item. I believe what you need is a simple preparation dictionary containing the number of tasks for each year before you enter the code block. Something like this for instance (code not tested):

 

// Create yearCount preparation dictionary
    NSMutableDictionary *yearCountDictionary = [NSMutableDictionary dictionary];
    for(NSObject *object in model.items)
    {
        NSString *year = [object valueForKey:@"year"];
        NSInteger yearCount = [(NSNumber *)[yearCountDictionary valueForKey:year] integerValue];
        yearCount++;
        [yearCountDictionary setValue:[NSNumber numberWithInteger:yearCount] forKey:year];
    }
    
    // Now set modelActions.sectionHeaderTitleForItem
    model.modelActions.sectionHeaderTitleForItem = ^NSString*(SCArrayOfItemsModel *itemsModel, NSObject *item, NSUInteger itemIndex)
    {
        NSString *year = [item valueForKey:@"year"];
        NSInteger yearCount = [(NSNumber *)[yearCountDictionary valueForKey:year] integerValue];
        NSString *yearHeader = [NSString stringWithFormat:@"%@ (%ld)", year, (unsigned long)yearCount ];
        return yearHeader;
    };


#16 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 22 December 2014 - 11:13 AM

It's also worth noting here that if you have a large number of objects (>10,000?), then it might be more efficient to create yearCountDictionary by directly access the Core Data store and fetching objects using predicates.



#17 David DelMonte

David DelMonte

    Forum Master

  • STV 4.0 Pro
  • PipPipPipPipPipPipPip
  • 527 posts
Reputation: 41
Excellent

Posted 22 December 2014 - 11:22 AM

Thanks Tarek.  will play with the code,  but I like the idea of using predicates. I guess I was on the wrong track although  It seems to me that sectionCount should not change cell aggregation. Without using it, I get what I want:

 

Attached File  Screen Shot 2014-12-22 at 2.20.14 PM.png   278.4K   11 downloads

 

 

 

Maybe later we can have some new/more STV magic..



#18 David DelMonte

David DelMonte

    Forum Master

  • STV 4.0 Pro
  • PipPipPipPipPipPipPip
  • 527 posts
Reputation: 41
Excellent

Posted 22 December 2014 - 11:33 AM

Thanks Tarek, the code snip you provided works perfectly. I'll use that until I get close to 10K calls... :)







Also tagged with one or more of these keywords: sections

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users