간단한 UITableView를 화면에 추가했습니다. 그러나 그 안에있는 셀에 사용자 정의 UI 요소 (주로보기 및 레이블)를 표시하고 싶습니다. UITableViewCell은 자유롭게 커스터마이징 할 수있는 기회를 많이주지 않기 때문에 필요한 모든 요소를 셀의 하위 뷰로 추가하기로했습니다. 여기 내 cellForRowAtIndexPath 메서드가 있습니다 :서브 뷰가있는 UITableViewCell이 재사용되지 않음
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 25.0f, 25.0f)];
view1.backgroundColor = [UIColor redColor];
view1.layer.shadowColor = [[UIColor blackColor] CGColor];
view1.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
view1.layer.shadowOpacity = 0.8f;
view1.layer.shadowRadius = 3.0f;
[cell addSubview:view1];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(85.0f, 5.0f, 100.0f, 15.0f)];
label1.text = @"dummy text 1";
label1.backgroundColor = [UIColor clearColor];
label1.textColor = [UIColor lightGrayColor];
label1.textAlignment = UITextAlignmentRight;
label1.font = [UIFont systemFontOfSize:12.0f];
label1.lineBreakMode = NSLineBreakByTruncatingTail;
label1.shadowOffset = CGSizeMake(0.0f, 1.0f);
label1.shadowColor = [UIColor blackColor];
[cell addSubview:label1];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(85.0f, 25.0f, 100.0f, 15.0f)];
label2.text = @"dummy text 2";
label2.backgroundColor = [UIColor clearColor];
label2.textColor = [UIColor lightGrayColor];
label2.textAlignment = UITextAlignmentRight;
label2.font = [UIFont systemFontOfSize:12.0f];
label2.lineBreakMode = NSLineBreakByTruncatingTail;
label2.shadowOffset = CGSizeMake(0.0f, 1.0f);
label2.shadowColor = [UIColor blackColor];
[cell addSubview:label2];
UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(85.0f, 45.0f, 100.0f, 15.0f)];
label3.text = @"dummy text 3";
label3.backgroundColor = [UIColor clearColor];
label3.textColor = [UIColor lightGrayColor];
label3.textAlignment = UITextAlignmentRight;
label3.font = [UIFont systemFontOfSize:12.0f];
label3.lineBreakMode = NSLineBreakByTruncatingTail;
label3.shadowOffset = CGSizeMake(0.0f, 1.0f);
label3.shadowColor = [UIColor blackColor];
[cell addSubview:label3];
return cell;
}
위의 코드는 셀 사용자 정의가가는 한 작동합니다. 문제는 셀이 가시 영역을 벗어나면 서브 뷰가 해제되지 않는다는 것입니다. 내가 테이블 뷰를 위아래로 움직이면, 추가 된 UIView의 그림자가 더 어둡고 어두워지고 레이블이 해제되지 않는다고 추측합니다.
이 문제를 어떻게 해결합니까? UITableViewCell 클래스를 서브 클래스로 만들 수 있다고 생각하지만, 셀의 클래스 내에 서브 뷰를 추가하고 싶습니다. 그것은 해결책으로 보이지 않습니다. 셀이 사라지면 셀 뷰를 하위 뷰로 만들거나 셀을 실제로 자유롭게 사용자 정의 할 수있는 확실한 방법이 있습니까?
감사합니다.
몇 가지 추가 정보 : 나는 IB를 사용하지 않는 내가 엑스 코드 4.6 내 SDK가
UITableViewCell ** 서브 클래 싱은 ** 해결책입니다. 당신이 지금하고있는 방식은 뷰와 라벨을 심지어 필요로하지 않는 셀에 추가 할 것입니다. –