1

I have a question regarding set up table view with hard coded data. Sometime developer face with situation when you need to display data that is received not form DATA MODEL layer, but stored as hard coded information.

What I mean For example when you build Settings screen, there can be some UITableViewCell like:

CustomTableViewCellName - Configuration data: placeholder name/surname, icon.

CustomTableViewCellPhone - Configuration data: phone placeholder, icon.

CustomTableViewCellLogout - Configuration data: logout/sign out text, icon.

Let's assume I have 3 table view section and each section contains 5 different cells that I described above as example of different cases.

if we will implement all these cells in

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section)
    ..... A LOT OF CASES THAT CONFIGURE EACH CUSTOM CELLS

As you see there is no DATA MODEL only placeholders and icon images that will be stored in UITableViewCell as a default data for the labels, textviews image views and etc. I don't say about model here as about model USER for example that stores user name, surname and etc. I mean that I need to store some default data somewhere, maybe in a dictionary. Actually a placeholder is just a placeholder, though it has a TEXT and TEXT is a data =)

In this case we have data for configure default state of views and data that will use for example as model USER.

What I worry about that I will have a big Switch and very Massive View Controller.

I would like to create few methods that will represent sections with a switch using indexPath.row in them:

 - (UITableViewCell *)cellsForSection1:(NSIndexPath *)ip
 - (UITableViewCell *)cellsForSection2:(NSIndexPath *)ip
 - (UITableViewCell *)cellsForSection2:(NSIndexPath *)ip

and move these methods to Interactor that will return for me a cell. I know that Interactor should prepare only data and push it over Presenter to a View.

Maybe I overthink a problem. I just don't want to have a big switch in VC =) I understand that there is no right solution. But maybe someone faced with it.

One more time in my case the data are the default cells that I want to configure somewhere to prevent overload view controller. Yea maybe it looks crazy that cells are data, but look at ViewController that contains 1000 lines of code for configuration cellForRowAtIndexPath is also crazy =)

Borys Verebskyi
  • 4,160
  • 6
  • 28
  • 42
Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277

3 Answers3

1

If your data is static, then instead of Switch cases I will prefer to use enumerations. Use Enums and in its body give tag to your each section and then in cellForRowAtIndexpath enumerate this Enum and display your data accordingly.

1

Consider creating a seperate class to act as your tableview data source and then have your view controller instance one. That way you avoid having an overly large VC class and your code is more easily unit testable.

habs93
  • 105
  • 2
  • 9
0
switch(model.id)
case cellKind1:
 cell = [tableview dequeue..];
//do 
 cell.data = model
//don't do like this
cell.title = model.title;
cell.abc = model.abc
Trung Phan
  • 923
  • 10
  • 18