UA-17470720-3

Jump to content


Photo
- - - - -

Two cell types in one table

cells core-data

  • Please log in to reply
7 replies to this topic

#1 mehdzor

mehdzor

    Starting Out

  • STV Lite
  • Pip
  • 4 posts
Reputation: 0
Neutral

Posted 26 October 2015 - 11:37 AM

Hello.

I have CoreData model where are two entities got the same parent. My goal is to show two different cells filled up with two different types of objects in the same tableView. 
Usually it could be easily achieved by using cellForRowAtIndexPath that returns cell type based on FRC current object. But how it can be done using STV? Is it possible to do with storyboard/xib or some other way? 



#2 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 26 October 2015 - 11:48 AM

Hi mehdzor,

 

Several ways come to mind, but the easiest in my opinion is to add both cells in the IB entity definition, then selectively remove the desired cell at runtime depending on the specific entity. The runtime code may look something like this:

 

// MasterViewController.m
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableViewModel.sectionActions.detailModelConfigured = ^(SCTableViewSection *section, SCTableViewModel *detailModel, NSIndexPath *indexPath)
    {
        SCTableViewSection *detailFirstSection = [detailModel sectionAtIndex:0];
        
        if(myCheck)
        {
            [detailFirstSection removeCellAtIndex:myFirstCellIndex];
        }
        else
        {
            [detailFirstSection removeCellAtIndex:mySecondCellIndex];
        }
    };
}

 

Hope this helps.



#3 mehdzor

mehdzor

    Starting Out

  • STV Lite
  • Pip
  • 4 posts
Reputation: 0
Neutral

Posted 26 October 2015 - 12:39 PM

I mean for array of objects when amount of objects is unknown. This example is for detail mode.

Hi mehdzor,

 

Several ways come to mind, but the easiest in my opinion is to add both cells in the IB entity definition, then selectively remove the desired cell at runtime depending on the specific entity. The runtime code may look something like this:

 

// MasterViewController.m
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableViewModel.sectionActions.detailModelConfigured = ^(SCTableViewSection *section, SCTableViewModel *detailModel, NSIndexPath *indexPath)
    {
        SCTableViewSection *detailFirstSection = [detailModel sectionAtIndex:0];
        
        if(myCheck)
        {
            [detailFirstSection removeCellAtIndex:myFirstCellIndex];
        }
        else
        {
            [detailFirstSection removeCellAtIndex:mySecondCellIndex];
        }
    };
}

 

Hope this helps.


Edited by mehdzor, 26 October 2015 - 12:40 PM.


#4 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 26 October 2015 - 12:50 PM

Oh so you need different cells for the SCArrayOfObjectsSection itself? If so, then you should supply them in sectionActions.cellForRowAtIndexPath:

 

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableViewModel.sectionActions.cellForRowAtIndexPath = ^SCCustomCell*(SCArrayOfItemsSection *itemsSection, NSIndexPath *indexPath)
    {
        SCCustomCell *customCell = nil;
        
        if(myCheck)
          customCell = [SCCustomCell cellWithText:nil objectBindingsString:nil nibName:@"MyFirstCustomCell"];
        else
          customCell = [SCCustomCell cellWithText:nil objectBindingsString:nil nibName:@"MySecondCustomCell"];
        
        return customCell;
    };
}

 

Please let me know if this is what you're looking for.



#5 mehdzor

mehdzor

    Starting Out

  • STV Lite
  • Pip
  • 4 posts
Reputation: 0
Neutral

Posted 26 October 2015 - 01:02 PM

Almost! Two more questions:
- How do I get a type of the object at this row? To select cell type based on it. I read whole documentation for SCArrayOfItemsSection and SCTableViewModel but found nothing similar to receiving current object. 
- How do I associate entity fields with cell labels and views when cell is created in xib? Where can I read about this? 

Oh so you need different cells for the SCArrayOfObjectsSection itself? If so, then you should supply them in sectionActions.cellForRowAtIndexPath:

 

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableViewModel.sectionActions.cellForRowAtIndexPath = ^SCCustomCell*(SCArrayOfItemsSection *itemsSection, NSIndexPath *indexPath)
    {
        SCCustomCell *customCell = nil;
        
        if(myCheck)
          customCell = [SCCustomCell cellWithText:nil objectBindingsString:nil nibName:@"MyFirstCustomCell"];
        else
          customCell = [SCCustomCell cellWithText:nil objectBindingsString:nil nibName:@"MySecondCustomCell"];
        
        return customCell;
    };
}

 

Please let me know if this is what you're looking for.


Edited by mehdzor, 26 October 2015 - 01:08 PM.


#6 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 26 October 2015 - 01:22 PM

a. Inside the action block, you can access the object using: [itemsSectio.items objectAtIndex:indexPath.row]. 

NSManagedObject *detailObject = (NSManagedObject *)[itemsSection.items objectAtIndex:indexPath.row];
NSString *entityName = [detailObject entity].name;

 

b. The easiest way to associate data to controls in custom cells is by using STV's 'Curly Brace Binding'. Using curly brace binding, you simply set the label's text to the property name you wish it to bind to, while enclosing it with curly braces, and STV will automatically bind the label's data to this property (e.g.: {propertyName} ). You can find a detailed example in our Custom Cells video, in addition to a detailed tutorial in the Exploring Web Service Binding part of STV's user manual. Finally, you can find several bundled STV samples that use this technique, including CalculatedCellsApp and WebServiceApp.


  • wizgod and mehdzor like this

#7 mehdzor

mehdzor

    Starting Out

  • STV Lite
  • Pip
  • 4 posts
Reputation: 0
Neutral

Posted 26 October 2015 - 02:47 PM

Thanks a lot for help!

a. Inside the action block, you can access the object using: [itemsSectio.items objectAtIndex:indexPath.row]. 

NSManagedObject *detailObject = (NSManagedObject *)[itemsSection.items objectAtIndex:indexPath.row];
NSString *entityName = [detailObject entity].name;

 

b. The easiest way to associate data to controls in custom cells is by using STV's 'Curly Brace Binding'. Using curly brace binding, you simply set the label's text to the property name you wish it to bind to, while enclosing it with curly braces, and STV will automatically bind the label's data to this property (e.g.: {propertyName} ). You can find a detailed example in our Custom Cells video, in addition to a detailed tutorial in the Exploring Web Service Binding part of STV's user manual. Finally, you can find several bundled STV samples that use this technique, including CalculatedCellsApp and WebServiceApp.



#8 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 26 October 2015 - 02:49 PM

You're most welcome! :)







Also tagged with one or more of these keywords: cells, core-data

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users