Question

I have created a custom XIB File with a UIView, which I wanted to be displayed in an UITableView as view for header in section. I properly implemented the required delegate methods, as of course:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:
  (NSInteger)section 
{
    if (tableView == self.messageTableView) {
        TableViewHeaderView *header = [[[NSBundle mainBundle] 
        loadNibNamed:@"TableViewHeaderView" owner:self options:nil] lastObject];

        [header.titleLabel setText:@"A String"];
        [header.addressLabel setText:@"Another String"];

        [header sizeToFit]; //I tried it with and without this line, 
                            //  does not seem to help
        return header;
    }
    return nil;
}

Thereby the TableViewHeaderView Class is a Subclass of UIView.

Everything seemed to work quite well, I actually did use this approach in some other projects as well and everything seemed to work well. Nevertheless I encountered a quite strange and random error: Running the app and playing around with the data of the Tableview, some Sectionheader-Views sometimes get displayed incorrectly like in the following screen. Due to my new account in this forum I cannot paste the picture directly into this post. But you can use the link instead (sorry for that):

http://i.stack.imgur.com/JSeVz.png

The UITableView has eight sections in this example. As you can clearly see the sectionheader-views "Sending 3,4,5" are totally misplaced. They are stuck to their position and move along when scrolling the tableview. However, when you scroll down the tableview they will appear a second time in the correct place.

By the way: I don't use AutoLayout in this project, the view has a correct height, corresponding to the heightForHeaderInSection: delegate method, and the same width as the underlying tableview itself. The Autosizing of the HeaderView as well as the underlying UITableView is set to:

http://i.stack.imgur.com/Ms0ZC.png

Reloading the tableview does not help either.

Does anyone have an idea what's wrong there? I don't have the slightest clue. Or is this approach of using custom views kind of old-fashined?

Thanks for your help!

Was it helpful?

Solution

I think i figured it out, it is a quite individual mistake though.

The error occurred in some cases when calling the reloadSections:withRowAnimation: Method on the concerned UITableView Object. Using reloadData solved the issue.

The mistake itself is difficult to explain, since this would mean to explain a big part of the concerned apps implementation. Anyway I think that it is caused by performing a lot of consequent reloadSections:withRowAnimation: mixed up with reloadData Method calls in a short time period. My app has several asynchronous calls to the reloadSections:withRowAnimation: method when new data is received through a UDP-Socket, and when in the meantime the user performs a tableview reload manually (when using a filter on the tableview - the buttons at the bottom of the view are used for that) this might actually cause a problem.

I think some already created Sectionheader-views objects might not get cleaned up so that they remain stuck to the superview. I'm not sure whether it is the right explanation, but the problem is fixed at least :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top