问题
I have a problem with some headers that are repeating on the screen. It seems to appear when I click in a textView in a TableViewCell, it scrolls automatically to put the cell in the middle of the screen, and I write in it during all that process. The header "repeated" can be in the footer, or anywhere else for info.
Here is the screenshot:
Here is the code:
- (NSString *)tableView:(__unused UITableView *)tableView titleForHeaderInSection:(NSInteger)index //HEADER
{
return [[self sectionAtIndex:index].header description];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *myLabel = [[UILabel alloc] init];
myLabel.frame = CGRectMake(12, 0, tableView.frame.size.width, 25);
myLabel.font = sectionFont;
myLabel.textColor = sectionColor;
myLabel.backgroundColor = colorSegment;
myLabel.text = [self tableView:tableView titleForHeaderInSection:section];
UIView *headerView = [[UIView alloc] init];
[headerView addSubview:myLabel];
return headerView;
}
回答1:
In textDidChange function, add that lines to prevent the animation to be finished or not before begining opération;
[CATransaction begin];
[CATransaction setCompletionBlock: ^{
[self.tableView beginUpdates];
[self.tableView endUpdates];
}];
[CATransaction commit];
回答2:
You shouldn't need to do anything to the view argument in willDisplayHeaderView, or forward anything to the 'delegate' in viewForHeaderInSection or heightForHeaderInSection (those are delegate methods so you are the delegate). heightForHeaderInSection should return a static value, not UITableViewAutomaticDimension, which can cause layout problems if not using auto layout. Your header views are retrieved by your 'sectionAtIndex' function, but it might be easier to use the UItableViewDataSource function 'titleForHeaderInSection' instead of 'viewForHeaderInSection', or generate UILabels on the fly.
来源:https://stackoverflow.com/questions/43682667/header-in-tableview-is-repeating-multiple-times