Search code examples
iosrubyuitableviewrubymotion

custom style for just the first row of the table view


I am trying to set a different style for the first row of my table view.

I'm doing this by the following code:

def tableView(table_view, cellForRowAtIndexPath: index_path)
  data_row = @data[index_path.row]
  puts index_path.row
  if index_path.row == 0
    cell = table_view.dequeueReusableCellWithIdentifier(ITEM_CELL_ID) || begin
      rmq.create(ItemCell, :first_cell, reuse_identifier: ITEM_CELL_ID).get
    end
  else
    cell = table_view.dequeueReusableCellWithIdentifier(ITEM_CELL_ID) || begin
      rmq.create(ItemCell, :rest_cell, reuse_identifier: ITEM_CELL_ID).get
    end
  end
  cell.update(data_row)
  cell
end 

Question

However I get very weird behavior with this. The first row does have the custom styling....but so does the 10th and 20th row!! I don't know why that would happen. Rows 2 - 9 and 11-19 are different than row 0 and 10 and 20.


Solution

  • You need to use different ITEM_CELL_ID (reuseIdentifiers) for each cell type. So the :first_cell styled cell should have a different reuseIdentifier constant than the :rest_cell styled cells. That should fix your issue since what you're seeing is that first cell's memory being reused over and over again as the table scrolls.