-4

Hi I am trying to make table view that has 5 cell and each cell automatically fills from the data or data type I have stored. here is the an example of what I have in mind, Please let me know if my question wasn't clear enough cheers.

Maybe I asked my question in a wrong way, I'll try to explain it more. So the Application I am making works this way: I have 5 different Drill type that each has different condition, and I need to make a different drill recording stats (UI) and tableView for each drill type,   So in the first table view that call Drills I have a cell that should show drill type and date that user made also one Create drill button that takes the user  to create drill (UI ), that has some attributes in it for example name, date , and DrillType(only 5 option),   After this I have to make 5 different table and scoringViewControl for each drill type specifically that they be able to insert their stats as many times as they want. And here I have problem, I don’t know what is the best way to manage navigating my cells in to the correct [drilltype] tableview.. (if I didn’t have to have some condition in create drill I would just made 5 statics Cell and navigate them into different direction)

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
    if([self.drill.drillType  isEqual: @"Grouping"] ){

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    Drill *drill = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = drill.drillType;
    cell.detailTextLabel.text = drill.drillDate;

    return cell;
    }
    else if([self.drill.drillType  isEqual: @"Normal"] ){

        static NSString *CellIdentifier = @"Cell2";
        UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        Drill *drill = [self.fetchedResultsController objectAtIndexPath:indexPath];
        cell2.textLabel.text = drill.drillType;
        cell2.detailTextLabel.text = drill.drillDate;

        return cell2;

    }
    return 0;
}
Alex.a
  • 21
  • 5
  • Also I should let you know that the code is completely working without if conditions – Alex.a Jun 01 '14 at 18:00
  • 2
    What is your question? I don't see one. – rdelmar Jun 01 '14 at 18:00
  • Actually this code won't work when I add If condition . – Alex.a Jun 01 '14 at 18:02
  • 2
    `Please let me know if my question wasn't clear enough cheers` -- what question? – nhgrif Jun 01 '14 at 18:03
  • libc++abi.dylib: terminating with uncaught exception of type NSException – Alex.a Jun 01 '14 at 18:04
  • here is the error I get. – Alex.a Jun 01 '14 at 18:04
  • What is different about your 5 cells? It looks like both the cells in your example have two labels you're filling -- is there any difference in the way they're set up, or is the only difference the strings that go into those labels? – rdelmar Jun 01 '14 at 18:06
  • Actually Yes, I need to navigate each cell into a different direction(with different entity on coreData) depend on the Drill type, that's why I need to show each drill type in a different cell. – Alex.a Jun 01 '14 at 18:10

1 Answers1

0

If you need want to have many cells you should use dynamic dispatch. Use dictionary to map type of cell to the key (if statement). Here is code

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  NSDictionary *cellIds = @{@"Grouping" : @"GroupingCellId", @"Normal" : @"NormalCellId"};

  Drill *drill = [self.fetchedResultsController objectAtIndexPath:indexPath];
  NSString *CellIdentifier = cellIds[drill.drillType];

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  [self configureCell:cell withObject:drill];

  return cell;
}

Also if you need to do some specific configuration for cells you could do it in 2 ways:

1 - Subclass a UITableViewCell and implement method that does cells configuration. different cells will implement this method in different ways. This technic is called "Polymorphism". Some explanation

  [cell setDrill:drill]  

2 - Make a dynamic dispatch (call a method) depending on what type is it. The same tech nick as for getting different cells for different type.

NSDictionary *configMethods = @{@"Grouping" : @"configGrouping", @"Normal" : @"configNormal"};
NSString *method = configMethods[drill.drillType];
[self performSelector:NSSelectorFromString(method) withObject:cell withObject:drill];
Community
  • 1
  • 1
Kostiantyn Koval
  • 8,407
  • 1
  • 45
  • 58