0

I am trying to build a form using a static UiTableViewController - I want to implement the IOS7 reveal method to display UIPickerView / UidateView inline, but its proving to be problematic due to the table being static rather than dynamic.

Anyhow - I've followed the solution by Aaron Bratcher in this question - iOS 7 - How to display a date picker in place in a table view? -

Looks like it would fit perfectly - my only issue is that my table cells are of varying heights - so I need to return either height of 200 or zero for the date picker row - but for everything else they need to keep their current height - I'm not sure whats the best way to code this - I need to reference the current cell and basically say stay as you are unless your the pickerRow?

This is what Ive got so far -

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 5 && indexPath.row == 1) { // this is my picker cell
        if (_editingStartTime) {
            return 220;
        } else {
            return 0;
        }
    } else {

         UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

        //return THE CURRENT CELLS HEIGHT
    }
}

Not sure how i'd access the height of cell above - or even whether that logic would do what i want it to do in heightForRowAtIndexPath? any ideas?

Cheers

Community
  • 1
  • 1
Dancer
  • 17,035
  • 38
  • 129
  • 206

3 Answers3

1

In a static tableView you can ask super for the height of the cell as it exists in the storyboard.

e.g.:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    /* ... */
    else {
        CGFloat height = [super tableView:tableView heightForRowAtIndexPath:indexPath];
        return height;
    }
}

same is true for the other dataSource methods, so you can probably remove some code from - tableView:numberOfRowsInSection: as well.

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
0

Try

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 5 && indexPath.row == 1) { // this is my picker cell
    if (_editingStartTime) {
        return 220;
    } else {
        return 0;
    }
} else {
   return self.tableView.rowHeight;
}

}

revolver
  • 2,385
  • 5
  • 24
  • 40
  • cheers @revolver - I tried that - it returns the same height for all rows - which isn't what I need as all cell heights differ – Dancer Jan 22 '14 at 12:15
  • where do you specify the height of the cells? if you want to return different height to the cell, you should be able to calculate and return them based on the indexPath? – revolver Jan 22 '14 at 12:23
  • 1
    For example, `if (indexPath.section == 0 && indexPath.row == 1) return 55` will give you a height of 55 for the cell in row 0 section 1 – revolver Jan 22 '14 at 12:24
  • cheers @revolver - i'm using a static table for design purposes - so all cell heights are defined in the storyboard - what you say there is a good idea - but I've 18 different cells - so wondered if there was a way to access the height property dynamically rather than having an if for each? – Dancer Jan 22 '14 at 13:00
  • Can you try to return `return cell.frame.size.height;` ? If you have set the height in storyboard this might work. – revolver Jan 22 '14 at 13:19
0

UITableViewCell class is a subclass of UIView, therefore it is basically a view which has its own frame. If you tell your table view cell to stay as it is, you should access its frame property:

return cell.frame.size.height;

But if you want that particular cell to have same height as other cells have, you should

return tableView.rowHeight;

as revolver had said. You might have set rowHeight property from Interface Builder, or you can explicitly set it in your code.

Mert Buran
  • 2,989
  • 2
  • 22
  • 34