2014-05-11 2 views
0

사용자 정의 UITableViewCell이 있고 셀 내에 오른쪽 하단과 오른쪽 상단 반경을 갖고 자하는 UIView이 있다고 가정 해보십시오. 이러한 변경 작업을 수행 할 수있는 올바른 위치는 어디입니까?UITableViewCell에서 UIView의 모양을 수정하는 올바른 방법은 무엇입니까?

나는 drawRect가 UITableViewCell에 없다고 가정하고 있습니다. 성능이 크게 떨어질 것이기 때문입니다. 이 작업을 수행하려면 UITableViewCell 내에 다른 기능이 있습니까? 아니면이 함수 내에서 내 UITableViewController에서이 일을해야

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

답변

1

당신이 cellForRowAtIndexPath 내에서 사용자 정의 셀을 인스턴스화 일단 내가 할 것 :이보기가 세포에 의해 소유되어 있기 때문에

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

NSString *cellIdentifier = @"cell"; 
UICustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    cell.customview.layer.cornerRadius = 5.0f; 
    //add other custom stuff here as well 
} 

return cell; 
} 
+0

당신은 설명 할 수 있습니까? –

+0

셀이 존재하지 않으면 셀에 대한 사용자 정의 수정을 실행합니다. 이것은 ** 선언 된 새 셀마다 한 번만 발생합니다. if 문 외부에서 수행하는 것과 반대되는 작업입니다. 사용자 정의 변경 사항이 표시되는 각 셀에 대해 더 많이 실행됩니다. – royherma

+0

그래서 셀을 재사용하고 있기 때문에 최초로 셀을 변경했을 때만 호출 할 것입니다. –

0

, I 셀의 init 메서드에서 수행 할 것이라고 - 나는 테이블 뷰의 데이터 소스 메서드에서 인덱스 경로에 의존하지 않는 (즉, 동적이 아닌) 뷰 설정 또는 수정을 유지하는 것이 가장 좋습니다. 어떤 init 메소드는 셀을 만드는 방법에 달려 있습니다. 스토리 보드에서 셀을 만든 경우 xib에 initWithCoder :,를 사용하고 initWithNibName : bundle :을 사용합니다. 또 다른 대안은 뷰 자체를 서브 클래스 화하고 init 메소드에서 수정하는 것입니다.

0

UITableViewCell 하위 클래스를 사용하고 사용자 지정 반경이있는 사용자 지정보기를 contentView에 추가하는 것이 좋습니다.

여기 내 간단한 구현입니다.

#import "TableViewCell.h" 
#import "RoundedView.h" 

@implementation TableViewCell 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     RoundedView *roundedView = [[RoundedView alloc] initWithFrame:CGRectInset(self.contentView.bounds, 2, 2)]; 
     [self.contentView addSubview:roundedView]; 
    } 
    return self; 
} 

@end 

그리고 반올림하여 필요한 모서리에 둥근 모서리를 설정합니다.

#import "RoundedView.h" 

@implementation RoundedView 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     self.backgroundColor = [UIColor clearColor]; 
    } 
    return self; 
} 


- (void)drawRect:(CGRect)rect{ 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    UIBezierPath *bp = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomLeft cornerRadii:CGSizeMake(10, 10)]; 
    CGContextAddPath(context, bp.CGPath); 
    CGContextClip(context); 
    [[UIColor brownColor] setFill]; 
    UIRectFill(rect); 

} 

@end 

그리고 cellForRowAtIndexPath 내부

:,보기 만 initWithStyle 내부의 셀에 추가되므로

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"]; 
    if(!cell){ 
    cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"]; 
    } 
    return cell; 
} 

참고 reuseIdentifier :있어서, 상기 뷰는 단지 새로운 셀이 할당되면 그리는 것이다. 대부분의 경우 UITableView는 셀을 대기열에서 제외하므로 몇 시간 만 그려집니다. 이 성능이 가장 좋은 것입니다 왜

그리고 최종 결과는 다음과 같습니다는

enter image description here

+0

drawRect 함수에서 드로잉 셀이 성능에 좋지 않으며 스크롤 속도가 느려질 수 있다고 생각합니다. –

+0

왜 아닙니다. 모든 UIView 클래스는 drawRect : 메서드에서 그 자체를 그립니다. 그러나 적절한 캐싱 및 다시 그리기를 피하면 성능이 크게 향상됩니다. – Sandeep

관련 문제