UA-17470720-3

Jump to content


Photo
- - - - -

Sorted Table View

cells

  • Please log in to reply
3 replies to this topic

#1 dmcgavin

dmcgavin

    Starting Out

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

Posted 16 December 2012 - 05:24 PM

I was wondering if anyone could help me with a problem.

I have an NSMutableArray of Objects that I use to fill a table view. If I don't use a search bar I can access the data from the array via the cellForRowAtIndexPath:indexPath. The user clicks the 5th row and the object is the 4th object in the array. Now that I am using the searchbar (which sorts and filters the data) the indexPath no longer corresponds to the correct object in the array. What is the correct way to do this? Do I have to use customCells?

Thanks in advance,
Doug

#2 dmcgavin

dmcgavin

    Starting Out

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

Posted 17 December 2012 - 04:09 PM

I worded my question poorly. So I will try again in the hopes that someone tells me what I am doing wrong.

So I have an object called a Scene

// Add some scenes

Scene *scene1 = [[Scene alloc] init];
scene1.sceneNumber = @"1";
scene1.sceneDescription = @"Scene 1";
scene1.ctlNbr = @"1";

Scene *scene2 = [[Scene alloc] init];
scene2.sceneNumber = @"2";
scene2.sceneDescription = @"Scene 2";
scene2.ctlNbr = @"2";

Scene *scene3 = [[Scene alloc] init];
scene3.sceneNumber = @"3";
scene3.sceneDescription = @"Scene 3";
scene3.ctlNbr = @"3";

[scenes addObject:scene1];
[scenes addObject:scene2];
[scenes addObject:scene3];

SCClassDefinition *sceneDef = [SCClassDefinition definitionWithClass:[Scene class] propertyNamesString:@":(sceneDescription)"];

sceneDef.titlePropertyName = @"sceneDescription";

SCArrayOfObjectsModel *objectsModel = [SCArrayOfObjectsModel modelWithTableView:self.tableView items:scenes itemsDefinition:sceneDef];

I set the model to the objectsModel.

When a row is selected how do I get the scene object back from the model? I need the ctlNbr value of the selected row.

Thanks,
Doug

#3 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 17 December 2012 - 07:04 PM

Hi Doug,

The problem is that once you start using the search bar, the displayed cells are only a subset of the total number of items you have in your SCArrayOfObjectsModel. For this reason, the filtered cells' indexPaths do not correspond to your ctlNbr anymore.

The correct way to get the ctlNbr value of the selected cell is via the cell's bound object (scene). This is how your code should look like:

...
objectsModel.cellActions.didSelect = ^(SCTableViewCell *cell, NSIndexPath *indexPath)
{
   Scene *selectedScene = (Scene *)cell.boundObject;
   NSString *selectedCtlNbr = selectedScene.ctlNbr;
};
...

Hope this helps.

#4 dmcgavin

dmcgavin

    Starting Out

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

Posted 17 December 2012 - 07:28 PM

Yes that helps very much Tarek. I was implementing the didSelectRowAtIndexPath method and had no idea how to get that. I have changed my code to yours and it works as expected now. Thank you very much.

:-)

Doug





Also tagged with one or more of these keywords: cells

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users