Hide a UITableView by setting row height to 0?

蹲街弑〆低调 提交于 2019-12-23 13:15:48

问题


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.


回答1:


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).




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/8313587/hide-a-uitableview-by-setting-row-height-to-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!