Header 在 TableView 中重复多次
Header in TableView is repeating multiple times
我有一些 header 在屏幕上重复出现的问题。当我单击 TableViewCell 中的 textView 时,它似乎会出现,它会自动滚动以将单元格放在屏幕中间,并且我在整个过程中都在其中写入。
header "repeated" 可以在页脚或其他任何地方提供信息。
截图如下:
代码如下:
- (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;
}
您不需要对 willDisplayHeaderView
中的视图参数执行任何操作,或将任何内容转发到 viewForHeaderInSection
或 heightForHeaderInSection
中的“delegate
”(那些是委托方法,所以你就是委托人)。 heightForHeaderInSection
应该 return 一个静态值,而不是 UITableViewAutomaticDimension
,如果不使用自动布局,这可能会导致布局问题。您的 header 视图由您的“sectionAtIndex
”函数检索,但使用 UItableViewDataSource
函数“titleForHeaderInSection
”而不是“viewForHeaderInSection
”可能更容易,或即时生成 UILabel。
在 textDidChange 函数中,添加防止动画在开始操作之前完成或未完成的行;
[CATransaction begin];
[CATransaction setCompletionBlock: ^{
[self.tableView beginUpdates];
[self.tableView endUpdates];
}];
[CATransaction commit];
我有一些 header 在屏幕上重复出现的问题。当我单击 TableViewCell 中的 textView 时,它似乎会出现,它会自动滚动以将单元格放在屏幕中间,并且我在整个过程中都在其中写入。 header "repeated" 可以在页脚或其他任何地方提供信息。
截图如下:
代码如下:
- (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;
}
您不需要对 willDisplayHeaderView
中的视图参数执行任何操作,或将任何内容转发到 viewForHeaderInSection
或 heightForHeaderInSection
中的“delegate
”(那些是委托方法,所以你就是委托人)。 heightForHeaderInSection
应该 return 一个静态值,而不是 UITableViewAutomaticDimension
,如果不使用自动布局,这可能会导致布局问题。您的 header 视图由您的“sectionAtIndex
”函数检索,但使用 UItableViewDataSource
函数“titleForHeaderInSection
”而不是“viewForHeaderInSection
”可能更容易,或即时生成 UILabel。
在 textDidChange 函数中,添加防止动画在开始操作之前完成或未完成的行;
[CATransaction begin];
[CATransaction setCompletionBlock: ^{
[self.tableView beginUpdates];
[self.tableView endUpdates];
}];
[CATransaction commit];