2013-11-20 2 views
1

셀의 왼쪽에 단추가 있고 버튼 오른쪽에 텍스트가있는 (별로) 사용자 지정 UITableView를 만들려고합니다.IOS - UITableViewCell에서 UILabel을 어떻게 이동합니까?

셀의 일부인 UILabel을 어디에 넣을 지 계산하려고 시도하지만 프레임을 변경하면 효과가 없습니다 (계산되지 않는 것처럼 보입니다 - 모두 0 임).

내 질문은 : 레이블의 프레임 크기는 언제 계산됩니까? (변경 사항이 적용됩니다).

올바른 방법인가요?

아래 코드 조각. 첫번째는 dequeueReusable ... 에 응답하여 호출 셀 초기화 (시작하고 있음 'buttonColor', 'onTapSel', 'buttonFrame'및 'buttonColor' "멤버 변수 (파일 정적 전역)

- (id)initWithStyle: (UITableViewCellStyle)style 
    reuseIdentifier: (NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 

    if (nil == buttonColor) 
    { buttonColor = kDefaultCellButtonColor; } 

    if (nil == onTapSel) 
    { onTapSel = @selector(defaultCellButtonTapped:); } 

    if (self) 
    { 
     EGCellButton * cellButton = (EGCellButton *)[[EGCellButton alloc ] initWithFrame:buttonFrame ]; 
     cellButton.backgroundColor = buttonColor; 
     [cellButton setTitle:buttonLabel forState:UIControlStateNormal]; 
     [cellButton addTarget:self action:(onTapSel) forControlEvents: UIControlEventTouchUpInside]; 
     [self setCellButton:cellButton]; 

     float xx = buttonFrame.origin.x + buttonFrame.size.width + 5; 
     CGRect frame = self.textLabel.frame; 
     frame.origin.x += xx; 
     self.textLabel.frame = frame; 
     [self addSubview:cellButton]; 
    } 
    return self; 
} 
은 cellForRowAtIndexPath에서

지금 :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"EGButtonTableCellID"; 
    EGButtonTableCell * cell = (EGButtonTableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    cell.cellButton.indexPath = indexPath; 

    // Configure the cell... 

    cell.textLabel.text = @"abcdefghijklmnopqrstuvwxyz"; 

    float xx = cell.cellButton.frame.origin.x + cell.cellButton.frame.size.width + 5; 
    CGRect frame = cell.textLabel.frame; 
    frame.origin.x += xx; 
    cell.textLabel.frame = frame; 

    return cell; 
} 

따라서, 버튼 (의도) 셀의 왼쪽에 표시하지만 라벨에 표시된 첫 글자 "g"인지되는 결과 처음 6 개의 문자는 버튼 뒤에 숨겨져 있습니다 (의도하지 않음).

프레임에 값을 덤프하면 origin.x를 제외한 모든 값이 두 방법에서 모두 0입니다.

그래도 될까요? 아니? 왜 안돼? 대단히 고마워요!

: BP : 사용자 정의의 layoutSubviews 방법에 UILabel의 및있는 UIButton에 대한

+0

당신이 설정하고있는 가치인가? – rdelmar

+0

'cellForRow ... '에서 커스텀 셀을 만드는 것은 큰 고통이다. 처음에는 더 쉽게 보이지만 UITableViewCell을 하위 클래스로 만들어야합니다. (매기의 대답을 따르십시오.) –

답변

1

설정 프레임있는 UITableViewCell

주제에

참조 애플 문서 :에 필요에 따라

서브 클래스는이 메소드를 오버라이드 (override) 할 수 있습니다 하위보기의 더 정확한 레이아웃을 수행하십시오. 서브 뷰의 자동 크기 조절 및 제한 조건 기반 동작이 원하는 동작을 제공하지 않는 경우에만이 메서드를 재정의해야합니다. 구현을 사용하여 서브 뷰의 프레임 사각형을 직접 으로 설정할 수 있습니다.

+1

** 중요 ** : 구현의 첫 번째 행으로'[super layoutSubviews]'를 호출해야합니다. – rmaddy

0

사용자 지정 UITableViewCell을 만들거나 클래스의 개체에 액세스하거나 메서드를 구현할 수 있습니다. buttonFrame의

CustomCell.h

#import <UIKit/UIKit.h> 

@interface CustomCell : UITableViewCell 

@property (nonatomic, retain) UILabel *customCellLabel; 
@property (nonatomic, retain) UIButton *customCellButton; 

@end 

CustomCell.m

#import "TLCell.h" 
@implementation CustomCell 

@synthesize customCellLabel, customCellButton 
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     customCellLabel = [[UILabel alloc] init]; 
     customCellLabel.frame = CGRectMake(180, 0, 60, 30); 
     [self addSubview:customCellLabel]; 

     customCellButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     customCellButton.frame = CGRectMake(0, 0, 60, 30); 
     [self addSubview:customCellButton]; 
    } 
    return self; 
} 

@end 

CustomTableView.m

/* --- */ 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellIdentifier = @"Cell"; 
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (cell==nil) { 
     cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 
    [cell.customLabel setText:@"hello"]; 
    // [cell. 
    [cell.customButton addTarget:self action:@selector(yourMethod:) forControlEvents:UIControlEventTouchUpInside]; 
} 
/* --- */ 
관련 문제