UA-17470720-3

Jump to content


Photo
- - - - -

STV generated detail view without STV


  • Please log in to reply
11 replies to this topic

#1 Matthew Purcell

Matthew Purcell

    Experienced Member

  • STV 3.0 Pro
  • PipPipPipPip
  • 84 posts
Reputation: 2
Good

Posted 29 December 2010 - 11:51 PM

Hi All,

The subject of this message is somewhat cryptic, so allow me to explain. While I am using STV for most of the table views within my app, a few of the table views are simply so customised (for one reason or another) that I unfortunately cannot use STV. However, I still need the same behaviour that STV offers in the way of detail views within these customised table views.

Is it possible to ask STV to generate detail views for the rows in a standard UITableView? The table view data is being sourced from Core Data, loaded into an array for the cell generation in the tableView:cellForRowAtIndexPath: method. I realise that I would need to handle quite a few things provided automatically by STV (e.g. indicating that the root table view needs to be updated when data within the detail views has changed, etc.) but I can manage those. I mainly just need the automatically generated detail views, so I don't need to "reinvent the wheel" and manually create detail views just for these non-STV table views.

Thanks,
Matthew

#2 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 30 December 2010 - 03:53 AM

Hi Matthew,

Sure, you can have STV work as the detail view of normal UITableView cells. However, before going into that, I wish if you can share with me why you're not using STV as your main table view. I really can't think of a case where STV can't be used instead of a normal UITableView, and I will most probably be able to help you to do just that. Of course, if you still just want to use a normal UITableView, please tell me and I'll give you details on how to achieve what you want :)

#3 Matthew Purcell

Matthew Purcell

    Experienced Member

  • STV 3.0 Pro
  • PipPipPipPip
  • 84 posts
Reputation: 2
Good

Posted 30 December 2010 - 04:28 AM

Hi Tarek,

Thanks for your quick reply. Don't get me wrong, STV is awesome for 99% of my application. However, in one table view I have done some very significant customisation to achieve specific behaviour for my use-case. Most specifically, I am using custom UITableViewDelegete and UITableViewDataSource classes and overriding quite a few of the standard methods. It would be difficult for me to go into the exact details here, but I need to generate the cells in a very particular way for the desired behaviour and thus have needed to use a standard UITableView.

I am in a bit of a hurry to get the app released, so at this point I think that I will need to stick with the standard UITableView with STV generating the detail view, so if you could provide some details on achieving this outcome that would be great.

[That said, in slower time, once the app has been released, I would welcome the opportunity to speak with you further about ways to get this unique table view across to using STV, as it would be good to get total uniformity of using STV across the app. Unfortunately I just don't have the time at the moment to re-do the existing work.]

While I have got you, a second (somewhat related) question. Say that I have a detail edit view for an STV which allows the user to edit all attributes of that row (it's bound to Core Data). For sake of argument, the attributes are First Name, Last Name, Age, Phone Number (the only mandatory attributes are First Name and Last Name). If the user taps the Add (+) button to add a new row, I only want them to be able to specify the First Name and Last Name in this Add view. Then, if they want to modify the Age and Phone number they must tap the newly created row to enter the detail edit view and do it through there. Is this possible?

Thanks again Tarek!
Matthew

#4 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 30 December 2010 - 10:44 AM

No problem Matthew, I understand :)

It's actually really simple to use STV from a normal UITableView.

1- Create the detail view controller subclass as you would normally do for your root view controller. The detail view should have the table model defined in its header file, then initialized in viewDidLoad (you will probably not need to add any sections to the model at this point). All this is exactly what you do when you use STV in the RootViewController, there is nothing new here. The only difference is that you should create a @property to give access to the tableModel from outside the class, as you'll probably need that later (see below). For the sake of this discussion, I'll refer to this detail view controller as DetailViewController.

2- In the view controller that uses the normal UITableView, implement the UITableViewDelegate method called didSelectRowAtIndexPath to push the DetailViewController.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  // I am assuming here that DetailViewController is a subclass of UITableViewController
  DetailViewController *myDetailView = [[DetailViewController alloc] initWithStyle:UITableViewStyleGrouped];

  // You'll probably need to add an SCObjectSection to the detail model
  SCObjectSection *objectSection = [SCObjectSection sectionWithHeaderTitle:nil
	withBoundObject:myObject withClassDefinition:myObjClassDef];
  [myDetailView.tableModel addSection:objectSection];

  // Push the detail view normally here (assuming self is within a UINavigationController)
  [self.navigationController pushViewController:myDetailView animated:TRUE];
  [myDetailView release];
}

That should be about it. Please tell me if you still have questions regarding this.

If the user taps the Add (+) button to add a new row, I only want them to be able to specify the First Name and Last Name in this Add view. Then, if they want to modify the Age and Phone number they must tap the newly created row to enter the detail edit view and do it through there. Is this possible?


The easiest way to do this is by implementing the SCTableViewModelDelegate method called detailViewWillAppearForSectionAtIndex and removing the automatically generated Age & Phone cells.

- (void)tableViewModel:(SCTableViewModel *)tableViewModel 
  detailViewWillAppearForSectionAtIndex:(NSUInteger)index 
  withDetailTableViewModel:(SCTableViewModel *)detailTableViewModel
{
  SCObjectSection *objectSection = (SCObjectSection *)[detailTableViewModel sectionAtIndex:0];
  
  // Assuming the cells order in the class definition is "First Name, Last Name, Age, Phone"
  [objectSection removeCellAtIndex:3]; // Phone Cell
  [objectSection removeCellAtIndex:2]; // Age Cell
}

Hope this helps!

#5 Matthew Purcell

Matthew Purcell

    Experienced Member

  • STV 3.0 Pro
  • PipPipPipPip
  • 84 posts
Reputation: 2
Good

Posted 02 January 2011 - 01:12 AM

Thanks Tarek :) I'll check this out and let you know how it goes.

By the way, happy new year to all!

Thanks,
Matthew

#6 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 02 January 2011 - 07:02 AM

Happy new year Matthew! :)

#7 Matthew Purcell

Matthew Purcell

    Experienced Member

  • STV 3.0 Pro
  • PipPipPipPip
  • 84 posts
Reputation: 2
Good

Posted 02 January 2011 - 07:20 PM

Thanks Tarek. Your instructions worked perfectly, although I did find that I had to put the tableModel initialisation tableModel = [[SCTableViewModel alloc] initWithTableView:self.tableView withViewController:self];
within the designated initializer for the detail view, otherwise it was just displaying a blank detail view (presumably because I was assigning the sections to the tableModel before it had been displayed and therefore before viewDidLoad was called which initialised the tableModel).

On a related question, would it be possible to call an STV generated Add (new Core Data object) view in a table view that is not managed by STV? That would solve the remaining part of the puzzle for me (as I have successfully implemented using STV for the Edit detail views in a non-STV managed table view, through following your excellent instructions, so I now just need to call the Add view).

Thanks,
Matthew

#8 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 03 January 2011 - 09:00 AM

Hi Matthew,

Your instructions worked perfectly, although I did find that I had to put the tableModel initialisation tableModel = [SCTableViewModel alloc] initWithTableView:self.tableView withViewController:self];
within the designated initializer for the detail view


Are you sure your DetailViewController subclasses SCTableViewController and not SCViewController?

would it be possible to call an STV generated Add (new Core Data object) view in a table view that is not managed by STV?


I actually have another idea that can magically solve both of your issues! :) The idea is to create an SCTableViewModel that will run silently in the background just as an engine that drives your application, but will not handle what the main UITableView displays (as per your requirements). This is actually a lot simpler than it sounds :) Here is how to do that:

1- In your main view controller (the one with the normal UITableView), create an SCTableViewModel as you normally would, but do not associate it with your UITableView. Then, add an SCArrayOfObjectsSection, again, as you normally would.

// viewDidLoad of the main view controller
- (void)viewDidLoad
{
...
// Create your entity class definition normally
SCClassDefinition *entityDef = [SCClassDefinition definitionWithEntityName:...];

// Initialize the tableModel but don't associate it with a UITableView
tableModel = [[SCTableViewModel alloc] initWithTableView:nil withViewController:self];

// Add an SCArrayOfObjectsSection as you normally would
SCArrayOfObjectsSection *objectsSection = 
  [SCArrayOfObjectsSection sectionWithHeaderTitle:nil withEntityClassDefinition:entityDef];
[tableModel addSection:objectsSection];
...
}

Now when you want to add a new object:

// You should assign this method as the action of your addButton
- (void)addButtonTapped
{
  SCArrayOfObjectsSection *objectsSection = 
	(SCArrayOfObjectsSection *)[tableModel sectionAtIndex:0];

  [objectsSection dispatchAddNewItemEvent];
}

And when a cell is selected:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  SCArrayOfObjectsSection *objectsSection = 
	(SCArrayOfObjectsSection *)[tableModel sectionAtIndex:0];

  [objectsSection dispatchSelectRowAtIndexPathEvent:indexPath];
}

That's it, no need for any of the code I had posted in my earlier post! :)

I've never tested this before, but it should work fine. Please tell me if you have any issues.

#9 Matthew Purcell

Matthew Purcell

    Experienced Member

  • STV 3.0 Pro
  • PipPipPipPip
  • 84 posts
Reputation: 2
Good

Posted 05 January 2011 - 01:33 AM

Hi Tarek,

Thanks for your reply. I'll give that a go.

Out of interest, if (for some arcane reason) I wanted to continue using the method you first described to push an STV detail edit view from a standard UITableView, would there be a similar way to produce the STV add view?

Thanks,
Matthew

#10 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 05 January 2011 - 08:05 AM

Hi Matthew,

Yes you can, but it will require a lot more work:

1- Create the managed object manually.
2- Pass it on to the detail view as I did in my first post.
3- When the detail view disappears, you'll have to check if the user canceled and delete the object.
4- If the application quits while the detail view is showing, you'll have to assume that the user canceled and delete the object.

I think the other idea I suggested is better :)

#11 Matthew Purcell

Matthew Purcell

    Experienced Member

  • STV 3.0 Pro
  • PipPipPipPip
  • 84 posts
Reputation: 2
Good

Posted 05 January 2011 - 11:00 PM

Hi Tarek,

Yes, I agree that the method you previously described is much better :)

However, you have now got me interested in one of the points which you mentioned:

3- When the detail view disappears, you'll have to check if the user canceled and delete the object.


How could I determine whether the detail view for a new (or edited) object was saved or canceled? Is there someway in a callback delegate to make this determination?

Thanks,
Matthew

#12 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 06 January 2011 - 07:42 AM

Hi Matthew,

How could I determine whether the detail view for a new (or edited) object was saved or canceled? Is there someway in a callback delegate to make this determination?


If you use our SCTableViewController, you'll be able to use the SCTableViewControllerDelegate method called "tableViewControllerWillDisappear:cancelButtonTapped:doneButtonTapped:" to figure that out. If you use your own UIViewController subclass, you'll be creating the "Cancel" and "Done" buttons yourself, and you'll be determining this manually inside the detail view controller.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users