我正在尝试将 CALayer 添加到我的 UITableViewCell 的底部以获得一点“阴影”效果。问题是当表格向上滚动并离开屏幕时,图层被删除,因此当您向下滚动时它是不可见的。如果您将单元格向下滚动到屏幕之外,然后再向上滚动,它们看起来很好。
I have a gif here showing what's happening.
我是这样做的:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:@"Row %d", (int)indexPath.row];
CALayer *bottomBorder = [CALayer layer];
bottomBorder.frame = CGRectMake(0.0f, cell.frame.size.height, cell.frame.size.width, 1.0f);
bottomBorder.backgroundColor = [UIColor colorWithWhite:0.9f alpha:1.0f].CGColor;
[cell.layer addSublayer:bottomBorder];
cell.clipsToBounds = NO;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
我还尝试为每个单元格使用唯一的 cellIdentifier,希望它们不会被重复使用,因此永远不会像这样删除该层:
//Same code as before just this changed
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"%ld_%ld", (long)indexPath.row, (long)indexPath.section]];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"%ld_%ld", (long)indexPath.row, (long)indexPath.section]];
}
//Same code continued...
所以我的问题是,当一个单元格被重复使用时,添加到它的 CALayer 被删除了。当单元格从屏幕上滚动并重新打开时,我需要做什么才能将该层保留在那里。
编辑:
我也试过这个也不管用:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
CALayer *bottomBorder = [CALayer layer];
bottomBorder.frame = CGRectMake(0.0f, cell.frame.size.height, cell.frame.size.width, 1.0f);
bottomBorder.backgroundColor = [UIColor colorWithWhite:0.9f alpha:1.0f].CGColor;
[cell.layer addSublayer:bottomBorder];
cell.clipsToBounds = NO;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.textLabel.text = [NSString stringWithFormat:@"Row %d", (int)indexPath.row];
return cell;
}
最佳答案
嘎嘎! 不要为每个单元格使用不同的单元格标识符。这完全破坏了单元格重用机制,并且会给你带来不断增加的内存使用,并迫使你总是为每个索引创建一个新单元格 - 最糟糕的结果。
每次单元格滚动到屏幕外时,它都会进入重用队列。然后,当新的索引路径滚动到 View 中时,系统会从回收站中取出以前使用过的单元格并将其交给您。您应该假设它的 STRUCTURE 已完全形成(您添加的任何自定义字段都将在那里)但内容都是错误的,需要完全设置。因此,如果您有一个包含 3 个标签的单元格,并且模型中的一些对象将文本填充到 1 个标签中,一些填充到 2 个标签中,一些填充到所有 3 个标签中,那么您应该始终为所有 3 个标签设置一个值,即使它是空的字符串。这可以防止上次使用单元格时留下的值遗留。
这有点像医生办公室,有病人表格供您填写。您填写一张表格并将其交给店员。职员将信息输入计算机,然后将表格放回空白表格堆的顶部。下一位患者必须删除表格中的所有内容,包括 child 的姓名(如果他们没有 child )、配偶、他们没有的既往病症等。否则,最后一个使用该工具的人的信息表单仍将在您的表单上。
至于你的阴影层,当你使用 dequeueReusableCellWithIdentifier 获取一个单元格时,你应该只在你必须创建一个新单元格时添加你的层。如果您取回一个重复使用的单元格,该层将已经存在,但如果它们在单元格之间发生变化,您可能需要设置它的内容。
关于ios - 添加 CALayer 到 UITableViewCell 滚动后移除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21342424/