Hide a UITableView by setting row height to 0?

2019-06-28 07:45发布

I just inherited code which hides/shows rows a UITableView by using the delegate heightForRowAtIndexPath method and returning height 0 for "hidden rows".

The code works, but it has me concerned there might be fraught with unforeseen complications. Can someone either ease my concerns or give me good reasons why this could cause problems (I couldn't find any issues with initial testing).

The table is fairly small <10 rows total and would require custom row heights even without this hidden row solution.

3条回答
冷血范
2楼-- · 2019-06-28 08:19

It would be cleaner to add and remove the rows between two beginUpdates and endUpdates calls, but I don't see why this 0-height method should not work.

If there are no UI-artifacts, that is (e.g. the Delete button showing up overflowing to the next cell).

查看更多
成全新的幸福
3楼-- · 2019-06-28 08:22

I do the same thing in the code I just worked on. I am not happy with different behaviour for different table view settings.
The alternative in my case is more complex (a model that adapts to what is visible or not).
For now, I put a //HACK comment on it and document a few peculiarities.
This is what I have found (iOS 5.0 tested):

  1. Set tableView.rowHeight = 1; Zero will give a cell with zero height (as returned by tableView:tableView heightForRowAtIndexPath:) some default height.
  2. You must have a cell separator. If none is selected, then a default height is assigned to zero height rows. The height of 1 is included with the separator.

If your code works in a different way, it would be interesting to know how it is set up.

查看更多
仙女界的扛把子
4楼-- · 2019-06-28 08:29

I use this method of setting hidden cell heights to 0. It works well and also means I can animate the inclusion of new cells by expanding the cell height (such as adding a DatePicker Cell like the calendar app does).

A few things I have had to watch out for in iOS 7.1 are that very squashed text does still appear even when a cell height is = 0 so I've needed to remove cell text in that case. Also, I have change the size of the cell's separatorInset as that was appearing as well.

查看更多
登录 后发表回答