开发者

Accessing "default" methods of UITableView

I try to set up a tableView. I use standard cells for all sections' rows except in the last section (containing one row). Thus, I would also like to use the standard layout for all those sections except that special one.

A short example is the following, my "special" cell is in section 3 (there is only one row):

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 3)
        return 5;
    return **????**;
}

At ??? I would like to return the width calculated from UITableView (just as if I did not implement the method).

[super self:tableView heightForHeade开发者_StackOverflow中文版rInSection:(NSInteger)section];

does not work. I know I can access

[tableView setionHeaderHeight]

which is by default 10 and obviously does not take into account that I have section headings for the other sections, which will require additional space. I tried that, but it will then get the sections too close (see screenshot):

(Note: the section I am interested in is the one which does not look like a cell: the one with the dates (invisible background)).

Accessing "default" methods of UITableView

So, the easiest thing would be to hand over the layout to the standard implementation which is perfect - except for section3.

What are my options?


Just in case: there is a new constant introduced in iOS 5, called UITableViewAutomaticDimension. As the documentation says, you should return it from your delegate method when you want UITableView to use a default value.

So, the code for your case would be:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 3) {
       return 5;
    } else {
       return UITableViewAutomaticDimension;
    }
}


You seem a bit confused about heightForHeaderInSection - it returns the height of a table section header (this is the "title" of a table section), not a row. iOS calls this method to ask for the height of just a single section header, irrespective of any other section headers there might be.

If you want to use the default, just return [tableView sectionHeaderHeight] for any section other than 3 - you don't need to "take into account that [you] have other section headers", as it's asking for the height of the header for section alone. It will ask again for the heights of others (and compute the relative positions with of rows and other sections automatically).


You do not have a super implementation tableView:heightForHeaderInSection: since you are not subclassing any abstract base implementation for UITableViewDelegate. The table view is instead decided if the default height should be used by inspecting your delegate implementation to see if the method is available.

It is a quite a huge concept to wrap your head around, especially if coming from Java or C#. Methods in Objective-C protocols can be optional, and their absence means use default.

Your method should probably be implemented as:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 3) {
       return 5;
    } else {
       return 36;
    }
 }

The default height for grouped and plain tableviews are different (22points for plain). The default values are not exposed by UITableView, not even as private methods. File bug at http://bugreport.apple.com to make this a public constant.


After overriding heightForHeaderInSection and doing a side-by-side comparison, the height for the header in the first row is larger than the rest. This isn't pixel perfect, but it's very close:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0 ) {
        return 46.0;
    } else if (section == myCustomRow) {
        return 12345.0; // custom height
    } else {
        return 36.0;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜