ios - 如何根据 UIlabel 文本大小动态调整单元格大小

标签 ios objective-c iphone uitableview

我是 iOS 初学者,我遇到了问题,如何将自定义单元格大小调整为文本的 UILabel 大小。我也无法在 UILabel 中完全显示长动态文本,它只显示选定的行,它只显示属性检查器中将显示的选定行。如何解决这两个问题。帮帮我

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_data count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    SingleTableViewCell *cell = (SingleTableViewCell *)[_mytable dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil){
        cell = [[SingleTableViewCell alloc]initWithStyle:UITableViewStylePlain reuseIdentifier:CellIdentifier];
    }
    cell.labelTitle.text=[_data objectAtIndex:indexPath.row];
    cell.labelcount.text=[NSString stringWithFormat:@"%u", (arc4random() % 74)];
    return cell;
}

这是我的表格委托(delegate)代码。我看到我的 CGrect 用于计算文本大小等,但我不会在这里工作。 我正在使用 Xcode 6.1.1 和 Objective-c 。

最佳答案

您可以使用 Autolayout 并将标签的边框设置为单元格,然后在您的 viewDidLoad 方法中执行此操作:

self.tableView.estimatedRowHeight = 44; // or your default size
self.tableView.rowHeight = UITableViewAutomaticDimension;

关于ios - 如何根据 UIlabel 文本大小动态调整单元格大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33938190/

相关文章:

ios - xCode - UIVisualEffectView 动画

ios - 如何在不替换旧应用程序的情况下安装新的 "version"应用程序?

iphone - 使用 NSStream 与 PHP 通信?

iphone - Swift 版本的 Obj.C 宏 : IS_RETINA

iphone - 在 iPhone 上显示格式化文本

ios - 在 Objective-C 项目中找不到 Swift 类

iOS-启动应用程序的方法

objective-c - Objective C 计算器编程变量

ios - 当状态从开/关更改时自定义 UISwitch 弹跳 - IOS-7 错误?

objective-c - 以编程方式选择 uitableview 行