Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:'
like image 684
mica Avatar asked Feb 15 '12 21:02

mica


1 Answers

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;    
  }

}
like image 110
mica Avatar answered Oct 01 '22 01:10

mica