0

I have a view with a table View. I has subclassed TableViewCells to make a representation of an object of my model. This Custom UITableViewCell has two buttons to change my object of the model(it makes +1 -1 to an instance variable).

So I think of how I should architecture all that, my options are:

-I put a pointer in the custom tableViewCell, when I receive a notification from one of the buttons I chance the cell and the object of the model(I don't like this solution because a View is changing my model, bad to reuse the cell so bad)

-Create a UICustomTableViewCellDelegate protocol and an instance of id<UICustomTableViewCellDelegate> in the cell class. When I receive a message from the UIElement I call my delegate. The delegate will point to the UIViewController of the general view who has an array with all the instances of my objects and here change the model. Before change the cell view to. I think is not bad for reuse but how I will identify and recover easily my object from the array of ViewController? I think the method should be:

-(void)customTableCellViewButtonPlusPressed:(CustomTableViewCell*) cell;

-Make a viewController for the CustomTableViewCell with a reference to my object. He will receive the message from the UIButtons of the cell and change the cellView and the object of the model referenced

What is the best practice?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Jpellat
  • 907
  • 7
  • 29

1 Answers1

1

Third solutions is the most mvc of them

  1. You will have a model, make it as heavy as possible with all the function and calculations to be done on the model itself
  2. The controller that is the UICustomTableViewCell, this class will read the model and realize it on the view, it will also get the events from the view and change the model accordingly
  3. and the view would be the UICustomTableViewCell xib file, that will contain the information of the cell drawing and realization

the model will be included inside UICustomTableViewCell in a "has a" relationship The UICustomViewCell class will be able to send drawing functions to the view and to receive events from the view and change the model accordingly

Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56
  • It seems a good answer to me. Only a question: My ViewCellController should have a reference to the CellView? I say that because the default relationship will be a UIView class object, when the TableView will ask for a TableViewCell in his datasource delegate it will be enough to cast the view object? like `return (UITableViewCell*)cellViewController.view` – Jpellat Jun 08 '12 at 12:07
  • 1
    nope, i would persent a function cellView that would be like this [cellViewController cellView] - (UITableViewCell *)cellView{ return (UITableViewCell*)self.view; } such way you would achieve better encapuslation of the view, so if you change the implementation of cellView in the future your ViewController will not need to change at all, you would just change the implementation of cellView function inside the CellController – Omar Abdelhafith Jun 08 '12 at 12:13
  • That's a perfect explanation. Thanks I like this design – Jpellat Jun 08 '12 at 12:16
  • 1
    welcome, it has been a pleasure helping you, please remember always increase the indirection and the encapsulation in all of your designs :) – Omar Abdelhafith Jun 08 '12 at 12:17