2012-11-05 4 views
0

각 셀 앞에 이미지 단추가있는 UITableView이 있는데 그 좌표는 UIButton으로 조정하고 싶습니다. 다음과 같이 cellForRow로 작성된 코드의 관련 부분은 다음과 같습니다표보기 셀의 이미지 단추 위치 변경

UIImage *image = [UIImage imageNamed "unchecked.png"]; 
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
CGRect frame1 = CGRectMake(0.0,0.0, image.size.width, image.size.height);** //changing the coordinates here doesn't have any effect on the position of the image button. 
button.frame = frame1; // match the button's size with the image size 
[button setBackgroundImage:image forState:UIControlStateNormal]; // set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet [button addTarget :self action: @selector(checkButtonTapped:event) forControlEvents:UIControlEventTouchUpInside]; 
+0

관련 코드는 관련 코드가 테이블 뷰 셀 캐싱을 제대로 처리했는지 여부입니다. –

+1

전체 cellForRowAtIndexPath 메소드를 작성하시오. –

+0

버튼을 어디에서 추가하고 있습니까? – DivineDesert

답변

0

UITableViewCell의 기본 레이아웃은 [accessoryView] [textLabel] [imageView]입니다. 너는 그것을 바꿀 수 없다.

UITableViewCell에 이미지를 임의로 배치하려면 UIImageView을 셀 contentView에 추가해야합니다.

+0

기본 레이아웃은 UITableViewCell을 서브 클래 싱하고 layoutSubviews 메서드를 덮어서 변경할 수 있습니다. –

0

보기 프레임을 설정하면 수퍼 뷰를 기준으로 위치가 설정되므로 프레임을 설정하기 전에 단추를 셀의 하위보기로 만들어야합니다.

그러나 이것은 cellForRowAtIndexPath에서 수행되어서는 안됩니다. 이는 테이블 뷰가 셀을 "재사용"할 때마다 새 버튼을 할당한다는 의미입니다. 단추를 만들고 테이블보기 셀을 초기화 할 때 프레임을 설정하여 셀당 하나의 단추 만 만들 수 있도록해야합니다.

그래서 원하는 것은 init 메소드가있는 UITableViewCell 서브 클래스입니다.

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     UIImage *image = [UIImage imageNamed:@"backgroundImage.png"]; 
     [self addSubview:button]; 
     [button setFrame:CGRectMake(0, 0, image.size.width, image.size.height)]; 
    } 
    return self; 
} 
관련 문제