Ios 自定义UITableViewCell内的UILabel位置
我有带有两个UILabel的自定义UITableViewCell(.h、.m和.xib文件)。我正在更改UILabel的内容,并希望相应地更改其位置。当我在更改代码中的位置后执行NSLogs时,它似乎正在工作,但UILabel的屏幕框架保持不变。我做错了什么Ios 自定义UITableViewCell内的UILabel位置,ios,uitableview,uilabel,Ios,Uitableview,Uilabel,我有带有两个UILabel的自定义UITableViewCell(.h、.m和.xib文件)。我正在更改UILabel的内容,并希望相应地更改其位置。当我在更改代码中的位置后执行NSLogs时,它似乎正在工作,但UILabel的屏幕框架保持不变。我做错了什么 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSSt
- (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;
}
您必须传递单元格的高度以及输入的文本
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
并将numberOfLines设置为label=0。可能重复我已经尝试了您的代码,它工作正常,您确定您的插座连接正确,或者是否有任何其他代码重置标签位置?好的,解决方案是勾选单元格.xib文件中的“使用自动布局”选项。。。这解决了我的问题。