分配委托以检测滚动后 TableView 单元格重叠
TableView Cells Overlap After Assigning Delegate to Detect Scrolling
这是 table 聊天视图。
我想添加一个按钮,当 table 视图没有滚动到底部时出现。
该按钮应用作按下时滚动到 table 视图底部的快捷方式。
在 viewDidLoad 中,我创建了一个渐变层 (UIView)、阴影层 (UIView) 和一个 UIButton。
- (void)viewDidLoad {
[super viewDidLoad];
//Create a gradient UIView
//Assuming color, size & other appearance properties are set here...
[self.view addSubview:gradientLayer];
//Create a button shadow UIView
//Assuming color, size & other appearance properties are set here...
[self.view addSubview:goToBtmShadow];
//Create a button that scrolls to bottom of table view
//Assuming color, size & other appearance properties are set here...
[self.view addSubview:goToBtm];
[self showGoToBtm:NO]; //Button is hidden by default
// ↓ TAKE NOTE OF THIS LINE
self.tableView.delegate = self; // <-- THIS LINE
// ↑ THIS LINE
}
在 scrollViewDidScroll 中,我将其设置为检查 table 视图是否滚动到底部以决定是否应显示按钮。
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
BOOL isScrolledToBottom = self.tableView.contentOffset.y >= (self.tableView.contentSize.height - self.tableView.bounds.size.height - 0.1);
if (isScrolledToBottom) {
[self showGoToBtm:NO];
} else {
[self showGoToBtm:YES];
}
}
创建 showGoToBtm 函数是为了让编码时的生活更轻松。
- (void) showGoToBtm:(BOOL)show {
if (show == true) {
gradientLayer.hidden = NO;
goToBtmShadow.hidden = NO;
goToBtm.hidden = NO;
goToBtm.enabled = YES;
} else {
gradientLayer.hidden = YES;
goToBtmShadow.hidden = YES;
goToBtm.hidden = YES;
goToBtm.enabled = NO;
}
}
如果不在 viewDidLod 中添加 self.tableView.delegate = self;
,无论如何按钮都不会出现。
但是当它最终被添加时,按钮的行为符合预期,但 table 视图开始以一种奇怪的方式表现。单元格未以正确的行高显示,内容相互重叠。
Screenshot before delegate is added
Screenshot after delegate is added
您确定问题出在这个按钮上吗?如果您只是删除 scrollViewDidScroll
问题是否仍然存在?
无论如何,您可能遇到的问题是,如果委托是 null
,则您的 table 视图使用的默认值可能在情节提要中设置并旨在自动调整大小。
请尝试将估计行高委托方法添加到某些值,例如 100
。并添加行高委托方法并将其设置为automatic
。或者在视图加载时直接在 table 视图上设置这两个。
这是 table 聊天视图。
我想添加一个按钮,当 table 视图没有滚动到底部时出现。
该按钮应用作按下时滚动到 table 视图底部的快捷方式。
在 viewDidLoad 中,我创建了一个渐变层 (UIView)、阴影层 (UIView) 和一个 UIButton。
- (void)viewDidLoad {
[super viewDidLoad];
//Create a gradient UIView
//Assuming color, size & other appearance properties are set here...
[self.view addSubview:gradientLayer];
//Create a button shadow UIView
//Assuming color, size & other appearance properties are set here...
[self.view addSubview:goToBtmShadow];
//Create a button that scrolls to bottom of table view
//Assuming color, size & other appearance properties are set here...
[self.view addSubview:goToBtm];
[self showGoToBtm:NO]; //Button is hidden by default
// ↓ TAKE NOTE OF THIS LINE
self.tableView.delegate = self; // <-- THIS LINE
// ↑ THIS LINE
}
在 scrollViewDidScroll 中,我将其设置为检查 table 视图是否滚动到底部以决定是否应显示按钮。
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
BOOL isScrolledToBottom = self.tableView.contentOffset.y >= (self.tableView.contentSize.height - self.tableView.bounds.size.height - 0.1);
if (isScrolledToBottom) {
[self showGoToBtm:NO];
} else {
[self showGoToBtm:YES];
}
}
创建 showGoToBtm 函数是为了让编码时的生活更轻松。
- (void) showGoToBtm:(BOOL)show {
if (show == true) {
gradientLayer.hidden = NO;
goToBtmShadow.hidden = NO;
goToBtm.hidden = NO;
goToBtm.enabled = YES;
} else {
gradientLayer.hidden = YES;
goToBtmShadow.hidden = YES;
goToBtm.hidden = YES;
goToBtm.enabled = NO;
}
}
如果不在 viewDidLod 中添加 self.tableView.delegate = self;
,无论如何按钮都不会出现。
但是当它最终被添加时,按钮的行为符合预期,但 table 视图开始以一种奇怪的方式表现。单元格未以正确的行高显示,内容相互重叠。
Screenshot before delegate is added
Screenshot after delegate is added
您确定问题出在这个按钮上吗?如果您只是删除 scrollViewDidScroll
问题是否仍然存在?
无论如何,您可能遇到的问题是,如果委托是 null
,则您的 table 视图使用的默认值可能在情节提要中设置并旨在自动调整大小。
请尝试将估计行高委托方法添加到某些值,例如 100
。并添加行高委托方法并将其设置为automatic
。或者在视图加载时直接在 table 视图上设置这两个。