-1

I have some dummy objects in my data source array.

I don't want to show them on screen. I also cannot remove them from data source array.

I am using proper constraints to get dynamic cell height.

Is there a way I can show a cell of 0 height for those dummy objects using AutoLayout?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 1
    There's some reason why you can not duplicate your datasource without those elements or in runtime decide which will have a cell assigned and just not add a cell for that dummy data? – Hugo Alonso Feb 08 '16 at 20:47
  • why couldn't you remove the dummy data by filtering? that makes no sense at all. – holex Feb 09 '16 at 16:25

2 Answers2

5

Instead of making a cell height = 0, you should be thinking about How to NOT show that cell.

If you can't duplicate your datasource without the dummy data (because is too big maybe?)

That's the easiest way by the way.

Then, you should build a function that can give you the correct data given an index.

For instance:

If you have [data1, dummy, data2, dummy, data3, data4]

If you ask for the second element of the datasource, it should respond data2 If you ask for the fourth element of the datasource, it should respond data4

Of course, you also needs a function that gives you the correct length of the datasource without all your dummy data.

Then just use this functions in proper places (for instance):

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return numberOfActualDataItems(datasource)
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = //Obtain your reusable cell

        let item = actualDataItemIndex(indexPath.row)
        //configureCell will be the function were you place your data to view
        self.configureCell(cell, item)
        return cell
}
Hugo Alonso
  • 6,684
  • 2
  • 34
  • 65
  • Does not fit my requirement.I simply cannot tweak the data source array anyway.I must have all the data at correct array index, since i have related most of cell indexPath with array index for further used logic in my code. – Swappy Dwappy Feb 09 '16 at 12:44
  • 1
    You can always refer to this functions, you can put them in an abstraction layer over your datasource. An use them when needed. – Hugo Alonso Feb 09 '16 at 15:57
  • 1
    @SwappyDwappy if your data is at the point that you **cannot change it in any way or it will break the app** then you need to change your data model because it is broken. This answer is a perfect (and very easy) solution to do what you want. – Fogmeister Feb 09 '16 at 16:03
  • @SwappyDwappy, I hardly believe you cannot filter your data array in runtime by non-dummy data. – holex Feb 09 '16 at 16:24
0

This is working so far,though i didn't wanted to override heightForRowAtIndexPath :(

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

 SomeObjectClass *object = [dataSourceArray objectAtIndex:indexPath.row];
 if (object.objectType == DummyDataType) {
    return 0;
 }
return UITableViewAutomaticDimension;
}
Hugo Alonso
  • 6,684
  • 2
  • 34
  • 65