当表格视图单元格中的标签需要在换行时保持一致,并且显示所有数据时,可以使用以下代码:
// 在 Table view datasource 中实现以下方法:
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
static NSString* cellIdentifier = @"cellIdentifier";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.textLabel.numberOfLines = 0; // 设置 label 可以换行
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping; // 设置换行模式
}
cell.textLabel.text = @"这里是需要显示的文字";
return cell;
}
在代码中,我们将表格单元格的标签设置为可以在换行时显示所有数据,并设置换行模式为 NSLineBreakByWordWrapping,以确保单元格标签在换行时具有一致性。