"그림자"효과를 얻기 위해 UITableViewCell의 아래쪽 부분에 CALayer를 추가하려고합니다. 문제는 테이블을 화면 위로 스크롤하여 화면에서 떼어 내면 레이어가 제거되므로 다시 스크롤하면 다시 볼 수 없습니다. 화면에서 셀을 아래로 스크롤 한 다음 백업하면 정상적으로 나타납니다. UITableViewCell에 CALayer를 추가 한 후 스크롤을 제거했습니다.
I have a gif here showing what's happening.
이
내가 그 일을하고있어 어떻게 : 나는 또한 그들이 다시 따라서되지 않을 것이라고 희망에 각 셀에 대한 고유 cellIdentifier를 사용하려고했습니다- (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;
}
레이어는 절대로 다음과 같이 제거 :
//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;
}
시간을내어 답변 해 주셔서 감사합니다. UITableView가 어떻게 셀을 재사용하는지 완전히 이해합니다. 내가 한 것은 잘못된 상태로 작동 상태로 전환하는 경우에도 문제를 일부 모양으로 수정하려는 시도였습니다. 나는 내가 묻는 것에 대한 명확한 정의로 나의 질문을 업데이트했다. – random
게시 한 코드가 틀린 것을 제외하고. 셀에 레이어를 추가하는 코드는 "if (! 셀)"조건부에 속합니다. 대신, 셀을 다시 사용할 때마다 새로운 레이어를 추가하는 것이 좋지 않습니다. 그것은 당신이 생각하는 것뿐만 아니라 세포 재사용을 이해하지 못한다는 것을 말해줍니다. –
제가 시도한 모든 코드를 게시 했어야합니다. 'if (! 셀)'에 넣어도 문제가 해결되지 않습니다. – random