Home Ask Login Register

Developers Planet

Your answer is one click away!

Swappy Dwappy February 2016

Give 0 height to table view cell using autolayout

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?


Hugo Alonso February 2016

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

Swappy Dwappy February 2016

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;

Post Status

Asked in February 2016
Viewed 1,039 times
Voted 14
Answered 2 times


Leave an answer

Quote of the day: live life