0
두 개의 UILabel을 사용하는 사용자 정의 UITableViewCell (.h, .m 및 .xib 파일)이 있습니다. 나는 UILabels의 내용을 바꾸고 그에 따라 그들의 위치를 바꾸고 싶다. 코드에서 위치를 변경 한 후 NSLogs를 실행하면 UILabel의 화면 프레임은 그대로 유지됩니다. 내가 도대체 뭘 잘못하고있는 겁니까?사용자 정의 UITableViewCell 내부의 UILabel 위치
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifierTwo = @"Cell with labels";
CellWithTwoLabels *cell = (CellWithTwoLabels *)[tableView dequeueReusableCellWithIdentifier:cellIdentifierTwo];
if (cell == nil) {
NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"CellWithTwoLabels" owner:nil options:nil];
for (UIView *view in views) {
if ([view isKindOfClass:[UITableViewCell class]]) {
cell = (CellWithTwoLabels *)view;
}
}
}
// First UILabel
cell.nameOfDataLabel.text = @"Region";
// Second UILabel
cell.valueOfDataLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15.0];
// Content of second UILabel
if (self.region) {
cell.valueOfDataLabel.text = self.region;
}
else {
cell.valueOfDataLabel.text = @"Placeholder text";
}
// Setting the frame of second UILabel
CGSize newSize = [cell.valueOfDataLabel.text sizeWithFont:[UIFont fontWithName:[NSString stringWithFormat:@"%@", cell.valueOfDataLabel.font.fontName] size:cell.valueOfDataLabel.font.pointSize]];
// Assign new size
CGRect textFrame = cell.valueOfDataLabel.frame;
textFrame.size = newSize;
textFrame.origin.x = cell.frame.size.width - textFrame.size.width - 20;
cell.valueOfDataLabel.frame = textFrame;
}
중복 가능성 (http://stackoverflow.com/questions/3163412/uitableview-with-dynamic-cell-heights) –
코드를 시도했는데 제대로 작동합니다. 콘센트를 올바르게 연결했는지 또는 다른 것이 있는지 확인하십시오. 레이블 위치를 다시 설정하는 코드? –
자, 해결책은 셀의 .xib 파일에서 "Use Autolayout"옵션을 체크하는 것이 었습니다 ... 그게 내 문제를 해결했습니다. – user1165156