UA-17470720-3

Jump to content


Photo
- - - - -

Sort table in reverse sequence?

models data-stores

  • Please log in to reply
10 replies to this topic

#1 David DelMonte

David DelMonte

    Forum Master

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

Posted 23 January 2015 - 05:16 PM

How do I sort a table in reverse sequence? I presume using this:

 

 

model.modelActions.sortSections = ^(SCTableViewModel *tableModel, NSMutableArray *sections)
{
// Rearrange the 'sections' mutable array here as you see fit
};
 

 

 

 

But how?? Thanks


Edited by David DelMonte, 23 January 2015 - 05:21 PM.


#2 wizgod

wizgod

    I'm what you guys call a User

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 575 posts
  • LocationThe Grid
Reputation: 149
Popular

Posted 23 January 2015 - 08:03 PM

Greetings Program!

 

I think this is what you:

 

objectsModel.modelActions.sortSections = ^(SCTableViewModel *tableModel, NSMutableArray *sections)
{
    // This is not the only way to sort, but probably the easiest when using STV
    SCDataFetchOptions *fetchOptions = [SCDataFetchOptions optionsWithSortKey:@"headerTitle" sortAscending:NO filterPredicate:nil];
	
    [fetchOptions sortMutableArray:sections];
};

 

Wg


Edited by wizgod, 24 January 2015 - 01:16 AM.

  • David DelMonte likes this

P.S. I love Swift... talk Swift.. Never too old school to learn yet another programming language. LOL! ;-)


#3 David DelMonte

David DelMonte

    Forum Master

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

Posted 23 January 2015 - 11:22 PM

hm.. thanks WG.. I only have one section, and don't know what it's called.. Is there another way? Thanks!!



#4 wizgod

wizgod

    I'm what you guys call a User

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 575 posts
  • LocationThe Grid
Reputation: 149
Popular

Posted 24 January 2015 - 12:15 AM

Sorry, when I saw the sortSections I thought you were sorting them in the table (for the sortSections, the sort key I found was the "headerTitle").

 

But for the rows in the section, I use sectionActions.didFetchItemsFromStore:

 

objectsModel.sectionActions.didFetchItemsFromStore = ^(SCArrayOfItemsSection *itemsSection, NSMutableArray *items)
{
	// This is not the only way to sort, but probably the easiest when using STV
	SCDataFetchOptions *fetchOptions = [SCDataFetchOptions optionsWithSortKey:@"name" sortAscending:NO filterPredicate:nil];

	[fetchOptions sortMutableArray:items];
};

 

Wg


Edited by wizgod, 24 January 2015 - 01:21 AM.

  • David DelMonte likes this

P.S. I love Swift... talk Swift.. Never too old school to learn yet another programming language. LOL! ;-)


#5 David DelMonte

David DelMonte

    Forum Master

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

Posted 24 January 2015 - 07:27 AM

Thanks for helping WG. I think I'm pushing an envelope here. The header cell is an SCCustomCell that is being used to expand/collapse a group. Each Custom Cell is an Expand/Collapse cell, for a year, and the "sub-cells" are records for that year. I appreciate any ideas..

 

Bigger question.. Did you pour through STV source code to understand how to do this? I need to learn how you and Ozie know so much, so that I can learn it too.



#6 wizgod

wizgod

    I'm what you guys call a User

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 575 posts
  • LocationThe Grid
Reputation: 149
Popular

Posted 24 January 2015 - 03:45 PM

I'm trying to replicate what you're doing; I have the custom E/C header cell added in modelActions.didAddSection. Where are you setting the values in the header field and how are you determining what the section header values are going to be?

 

Wg


P.S. I love Swift... talk Swift.. Never too old school to learn yet another programming language. LOL! ;-)


#7 David DelMonte

David DelMonte

    Forum Master

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

Posted 24 January 2015 - 08:41 PM

WG. Here is a dump of part of ViewDidLoad -- I'll leave it up for a few days.. Thanks again for looking and let me know if you see something odd..


 

//Section Expand/collapse

 

model.modelActions.didAddSection = ^(SCTableViewModel *tableModel, SCTableViewSection *section, NSUInteger sectionIndex)

{

SCExpandCollapseCell *cell = [SCExpandCollapseCell cellWithExpandText:section.headerTitle collapseText:section.headerTitle ownerSectionExpanded:YES];

 

cell.backgroundColor = [colors objectAtIndex:decade];

cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:20];

counter = section.cellCount;

section.expandCollapseCell = cell;

 

UIImage * image = [ UIImage imageNamed:@"downTriangle.png" ] ;

UIControl * c = [ [ UIControl alloc ] initWithFrame:(CGRect){ CGPointZero, image.size } ] ;

 

c.layer.contents = (id)image.CGImage ;

 

cell.accessoryView = c ;

 

};

 

 

 

 

//YearNumber

 

model.cellActions.willConfigure = ^(SCTableViewCell *cell, NSIndexPath *indexPath) {

 

// Create yearCount preparation dictionary

 

 

 

DDMemories *memory = (DDMemories *)cell.boundObject;

NSString *tempYear = [memory valueForKey:@"year"];

 

NSNumberFormatter * f = [[NSNumberFormatter alloc] init];

[f setNumberStyle:NSNumberFormatterDecimalStyle];

 

NSNumber * myNumber = [f numberFromString:tempYear];

 

[memory setValue:myNumber forKey:@"yearNumber"];

 

self.tableView.separatorColor = [UIColor blackColor];

 

 

};

 

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

yearCounter = [(NSNumber *)[yearCountDictionary valueForKey:year] integerValue];

yearCounter++;

//NSLog(@"yearCounter 2: %li", (long)yearCounter);

[yearCountDictionary setValue:[NSNumber numberWithInteger:yearCounter] forKey:year];

 

 

model.sectionActions.didFetchItemsFromStore = ^(SCArrayOfItemsSection *itemsSection, NSMutableArray *items)

{

// This is not the only way to sort, but probably the easiest when using STV

SCDataFetchOptions *fetchOptions = [SCDataFetchOptions optionsWithSortKey:@"year" sortAscending:NO filterPredicate:nil];

 

[fetchOptions sortMutableArray:items];

};

 

 

// header Title

 

model.modelActions.sectionHeaderTitleForItem = ^NSString*(SCArrayOfItemsModel *itemsModel, NSObject *item, NSUInteger itemIndex)

{

 

 

NSString *year = [item valueForKey:@"year"];

NSInteger yearInt = [year integerValue];

NSInteger age = yearInt - birthYear;

 

decade = age / 10;

if (decade < 0) decade = 0;

 

yearCounter = [(NSNumber *)[yearCountDictionary valueForKey:year] integerValue];

 

NSString *countString;

if (yearCounter == 1) {

countString = @"record";

} else {

countString = @"records";

}

 

 

NSString *yearHeader;

 

if (_IPAD) {

yearHeader = [NSString stringWithFormat:@"%@   %ld %@   age: %ld", year, (unsigned long)yearCounter, countString, (unsigned long) age ];

} else {

yearHeader = [NSString stringWithFormat:@"%@      %ld %@     age: %ld", year, (unsigned long)yearCounter, countString, (unsigned long) age ];

}

 

 

return yearHeader;

};

 

[model.modeledTableView reloadData];

 

 

//Display

 

model.cellActions.willDisplay = ^(SCTableViewCell *cell, NSIndexPath *indexPath)

{

NSInteger year = [(NSNumber *)[cell.boundObject valueForKey:@"year"] integerValue];

 

NSInteger age = year - birthYear;

NSString *ageString = [NSString stringWithFormat:@"%li", (long)age ];

[cell.boundObject setValue:ageString forKey:@"age"];

 

cell.accessoryType = UITableViewCellAccessoryNone;

};

 

 

self.tableViewModel = model;

 


Edited by David DelMonte, 24 January 2015 - 08:46 PM.


#8 wizgod

wizgod

    I'm what you guys call a User

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 575 posts
  • LocationThe Grid
Reputation: 149
Popular

Posted 25 January 2015 - 11:11 AM

Can you show me a screenshot of your nested E/C tableview?

 

I think I have it working for the most part; I added the concept to one of my projects (although it's in Swift) but I may be missing one of your nested E/C levels.

 

https://www.dropbox....G_8522.PNG?dl=0

https://www.dropbox....G_8524.PNG?dl=0

 

If you need to do a sort on a calculated value that you have set in willConfigure or willDisplay, what I have done in one of my other projects is loop through the items in didFetchItemsFromStore, made the calculations, set the property and then did the sort.

 

Wg


P.S. I love Swift... talk Swift.. Never too old school to learn yet another programming language. LOL! ;-)


#9 David DelMonte

David DelMonte

    Forum Master

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

Posted 25 January 2015 - 02:30 PM

Here are two images...

 

Attached File  Screen Shot 2015-01-25 at 5.27.04 PM.png   68.28K   3 downloads

 

Attached File  Screen Shot 2015-01-25 at 5.27.23 PM.png   65.61K   3 downloads

 

 

 



#10 David DelMonte

David DelMonte

    Forum Master

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

Posted 25 January 2015 - 02:31 PM

But this is a value from the model (year). It's not calculated.. 



#11 wizgod

wizgod

    I'm what you guys call a User

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 575 posts
  • LocationThe Grid
Reputation: 149
Popular

Posted 25 January 2015 - 04:19 PM

I'm really starting to hate this Magic Mouse; one accidental swipe and back or forth you go and lose what you wrote. Will save and edit this time :-)

 

For some reason, mine shows the header title in the actual header still...

 

I have it working like you want I think; what I did was use a nib for the header and set it in didAddSection.

 

Nib: https://www.dropbox....erCell.xib?dl=0

 

This is the class for it:

 

class WGExpandCollapseHeaderCell:SCExpandCollapseCell
{
    var year:String!
    var numberOfRecords:String!
    var age:String!
    
    @IBOutlet var yearLabel : UILabel!
    @IBOutlet var numberOfRecordsLabel : UILabel!
    @IBOutlet var ageLabel : UILabel!

    override var ownerSectionExpanded:Bool {
        didSet {
            refresh()
        }
    }
    
    func refresh()
    {
        if year != nil {
            // Just reversing the string to make sure it's working.
            yearLabel.text = ownerSectionExpanded ? year : year.reverseString()
        }
        
        if numberOfRecords != nil {
            numberOfRecordsLabel.text = numberOfRecords
        }
        
        if age != nil {
            ageLabel.text = age
        }
    }
}

 

self.tableViewModel.modelActions.didAddSection = {
    (tableModel, section, sectionIndex) in

        // Use our nib for the custom E/C cell.
	var headerCell = NSBundle.mainBundle().loadNibNamed("HeaderCell", owner: self, options: nil).first as WGExpandCollapseHeaderCell

        // Starting state of the E/C cell.
	headerCell.ownerSectionExpanded = true

	section.expandCollapseCell = headerCell
}

 

model.modelActions.sectionHeaderTitleForItem = ^NSString*(SCArrayOfItemsModel *itemsModel, NSObject *item, NSUInteger itemIndex)
{
	// Return the year for the headerTitle since this is what you want to sort by.
	return [item valueForKey:@"year"];
};

 

model.modelActions.sortSections = ^(SCTableViewModel *tableModel, NSMutableArray *sections)
{
    // Since we returned the year in the headerTitle, we can sort by that property.
    SCDataFetchOptions *fetchOptions = [SCDataFetchOptions optionsWithSortKey:@"headerTitle" sortAscending:NO filterPredicate:nil];
	
    [fetchOptions sortMutableArray:sections];
};

 

self.tableViewModel.cellActions.willDisplay = {
	(cell, indexPath) in
	
	if cell.isKindOfClass(WGExpandCollapseHeaderCell)
	{
		var objectsSection = cell.ownerSection as SCArrayOfObjectsSection
		var headerCell = cell as WGExpandCollapseHeaderCell
		
		// The year comes from sectionHeaderTitleForItem.
		headerCell.year = cell.ownerSection.headerTitle
		
		// We use the items.count because section.cellCount gives the number of cells presently in the section and not items.
		headerCell.numberOfRecords = "\(objectsSection.items.count-1)"
		
		// Your calculated value here.
		headerCell.age = "\(Int(rand()) % 65)"

		// Update the labels.
		headerCell.refresh()
	} else if cell.isKindOfClass(SCCustomCell) {
            // Display for the rest of the cells...
        }
}

 

self.tableViewModel.cellActions.didSelect = {
    (cell, indexPath) in

    // Don't take any action if the custom E/C cell is selected.			
    if !cell.isKindOfClass(WGExpandCollapseHeaderCell)
    {
        self.performSegueWithIdentifier("topics", sender: cell)
    }
}

 

Sorry for the mixed Switch/Obj-C.

 

Wg


Edited by wizgod, 25 January 2015 - 09:00 PM.

  • David DelMonte likes this

P.S. I love Swift... talk Swift.. Never too old school to learn yet another programming language. LOL! ;-)






Also tagged with one or more of these keywords: models, data-stores

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users