mica
mica

Reputation: 4308

Including a section with "dynamic prototypes" in a static Tableview with "static cells"

I want to define a static tableview with one dynamic section Is that possible?

section 0 shall be static, the lables are wired in xcode with the outlets.

section 1 shall be dynamic

I tried this, but I don´t know what cell I shall return for the static part.

static NSString *CellIdentifier = @"ItemCellBasic";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

switch (indexPath.section)
{ case 0:
    return // I don´t know what

  case 1:
    cell.textLabel =@"dynamic";
    return cell;    
}

EDIT 1; now I tried:

case 0: return [super tableView:tableView cellForRowAtIndexPath:indexPath];

but got:

*** Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:6072
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

Upvotes: 9

Views: 6489

Answers (2)

Jose Munoz
Jose Munoz

Reputation: 558

instead of a switch try using some good old if statements

 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
       if(section==STATIC_SECTION){
           return  *the number of rows in the static section*
       }
       else{
           return NUMBER_OF_DYNAMIC_ROWS; 
       }
  }

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

    static NSString *CellIdentifier = @"ItemCellBasic";
    UITableViewCell *yourCell = [tableView dequeReusableCellWithIdentifier:CellIdentifier forIndexpath:indexPath];

    yourCell.text = *your dynamic text or something*

    return yourCell;
}

now assuming your static cells don't have reuse identifiers, cause that would just be redundant, and since you only need one prototype cell for reuse this should be your setup

Upvotes: -1

mica
mica

Reputation: 4308

I have a partial solution for this problem

In the Table view data source methods I return the superclasses result for the static cells, for the dynamic cells I return the needed dynamic values.

In - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath the dequeueReusableCellWithIdentifier: returns nil so I create a new UITableViewCell

The remaining problem:

in xcode you have to specify, the number of rows in the "dynamic section" (whitch is ofcourse not dynamic). You cann´t display more than the maximum you defined here (or get an exception ;-)).

Samplecode:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{ 
  switch (section) 
  { case STATIC_SECTION:
      return  [super tableView:tableView numberOfRowsInSection:section];

    case DYNAMIC_SECTION
      return NUMBER_OF_DYNAMIC_ROWS; 
  }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *CellIdentifier = @"ItemCellBasic";
  UITableViewCell *cell; 

  switch (indexPath.section)
  {
    case STATIC_SECTION:
      return [super tableView:tableView cellForRowAtIndexPath:indexPath];

    case DYNAMIC_SECTION:
      cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (!cell) 
      {  cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
      }

      cell.textLabel.text=DYNAMIC_TEXT;
      return cell;    
  }

}

Upvotes: 5

Related Questions