셀의 왼쪽에 단추가 있고 버튼 오른쪽에 텍스트가있는 (별로) 사용자 지정 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에 대한
당신이 설정하고있는 가치인가? – rdelmar
'cellForRow ... '에서 커스텀 셀을 만드는 것은 큰 고통이다. 처음에는 더 쉽게 보이지만 UITableViewCell을 하위 클래스로 만들어야합니다. (매기의 대답을 따르십시오.) –