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):
https://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:
https://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!