2

I have a static UITableView and want to hide a table cell (exactly I want to hide the datepicker in the cell). I want to do this in the heightForRowAtIndexPath method:

public override float GetHeightForRow (UITableView tableView, NSIndexPath indexPath)
{
    float height = this.TableView.RowHeight;

    if (indexPath.Row == 5 || indexPath.Row == 7) {
        height = 0.0f;
    }

    return height;
}

When I'm doing this the content of the cell overlaps with the other content. In my case the datepicker takes the whole screen. The same goes for labels and so on. Also it seems that the separator line is still there.

How can I remove or hide a cell with it's content temporarily?

You don't have to provide a C# solution. I will translate it by myself. BTW: I'm using iOS 7.1

testing
  • 19,681
  • 50
  • 236
  • 417

3 Answers3

3

Check "Clip Subviews" to your cell in IB. not for "Content View" of cell.

To arhchive this by code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
    if(indexPath.row == 5 || indexPath.row == 7) {
        cell.clipsToBounds = YES;
    }
    return cell;
}
rintaro
  • 51,423
  • 14
  • 131
  • 139
  • I tried to set this on my cells I want to hide. Still overlapping but not as much. In my case three labels from three cells are overlapping. – testing Sep 18 '14 at 09:19
  • 1
    Edited the answer, hope this solves the problem. BTW, For static cells, `UITableViewController` manages the datasource and the delegate. So you should call `[super tableView:tableView heightForRowAtIndexPath:indexPath]` for default behavior. – rintaro Sep 18 '14 at 09:26
  • If I use `cellForRowAtIndexPath` then I don't have static cells anymore or? Now I actived `Clip Subviews` for the cell, the view and the datepicker. Still I have overlapping. – testing Sep 18 '14 at 09:26
  • Don't worry, `[super tableView:tableView cellForRowAtIndexPath:indexPath]` returns your static cells. OK, i will check this with iOS 7.1. – rintaro Sep 18 '14 at 09:29
  • Hooray! I'll test it more but it seems that it now works. I'll now try to show it again. This is what I've done: 1. Activated `Clip Subviews` on the cell and the date picker. 2. Called `super`/`base` for cells which shouldn't change in height. 3. Now using 4 and 6 for `indexPath.Row`. – testing Sep 18 '14 at 09:37
0

i would try to reduce numberOfRowsInSection: and consider this in cellForRowAtIndexPath:, but i'm not 100% sure if that works.

donmarkusi
  • 346
  • 2
  • 13
  • You mean that I should switch from static table view cells to dynamic prototype cells? – testing Sep 18 '14 at 08:51
  • 1
    Yes this works with static cells. But it's too complicated I think. You have to **adjust** `indexPath` with whether the cell is displaying or not, and call `super` with adjusted `indexPath` for **every** datasource/delegate method. But if you want cell remove/insert effects by `UITableView`, that is the only way. – rintaro Sep 18 '14 at 09:59
0

I recommend you to update the method-

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
     if (indexPath.Row == 5 || indexPath.Row == 7) {
        return 0.0f;
     }
    return 50; // some static value
}

I hope this will solve your problem.

Kaps18
  • 156
  • 6
  • That didn't changed the screen. The elements are still overlapping. – testing Sep 18 '14 at 08:53
  • Please provide more info what you want, because same is working fine with me. – Kaps18 Sep 18 '14 at 08:56
  • I'm following this tutorial http://masteringios.com/blog/2013/11/18/ios-7-in-line-uidatepicker-part-2/2/ but everything in C# (but that's not the problem here). I added a datepicker to my `UITableViewCell` and now I want to hide this cell (and later show it again). I'm stuck at this part *If we run our project now the date picker is gone*. The picker isn't gone. It is still overlapping. If you'd like I could post a screenshot. The problem is similar to this post: http://stackoverflow.com/a/22352329/426227 – testing Sep 18 '14 at 09:05