ios - 在 IOS 8 中使用自动布局调整单元格大小

标签 ios objective-c iphone uitableview autolayout

我曾尝试使用内容大小调整单元格大小,但我了解到在 IOS 8 中您可以使用自动版式调整单元格大小。我尝试了同样的方法,但无法获得确切的用户界面。我在 UIView Controller 中有一个 TableView。这是我的代码 viewcontroller.m :-

@implementation MessageViewController
@synthesize  messageTable;
- (void)viewDidLoad {
    self.messageTable.estimatedRowHeight = 70.0;
    self.messageTable.rowHeight = UITableViewAutomaticDimension;

   [super viewDidLoad];


  if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
      self.edgesForExtendedLayout = UIRectEdgeNone;
  self.navigationController.toolbarHidden=NO;
  _txtField=[[UITextView alloc] initWithFrame:CGRectMake(40, 5, 220, 30)];
  [_txtField setFont:[UIFont systemFontOfSize:15]];
  _txtField.layer.cornerRadius = 7.0;
  _txtField.clipsToBounds = YES;

  _txtField.text=@"Hello";



[self.navigationController.toolbar addSubview:_txtField];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"Send" forState:UIControlStateNormal];
button.frame = CGRectMake(265.0, 5, 50.0, 30.0);

[self.navigationController.toolbar addSubview:button];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]

                               initWithTarget:self
                               action:@selector(dismissKeyboard)];

[self.view addGestureRecognizer:tap];


// table view data is being set here
_messageArray = [[NSMutableArray alloc]initWithObjects:
           @"Data 2 ",@"Data 1 Data 1Data 1Data 1Data 1Data 1Data 1Data     1Data 1D",@"Data 8 ",
               @"Data 9 ", nil];
     [self.messageTable reloadData];

// Do any additional setup after loading the view.
}

UI 看起来像这样:-

enter image description here

标签上的约束是:- enter image description here

需要帮助:)

编辑:-

enter image description here

单元格

enter image description here

最佳答案

http://www.appcoda.com/self-sizing-cells/ 您可以尝试此链接以仅在 iOS 8 中自动调整标签大小。

并且不要忘记在您的 viewdidload 中添加以下行。

tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension

关于ios - 在 IOS 8 中使用自动布局调整单元格大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28347001/

相关文章:

ios - 如何在 Swift 中以编程方式更改 UICollectionViewCell 大小?

objective-c - 没有名为 wkextensiondelegate 的类型或协议(protocol)

ios - 通过在方法名中使用变量来调用方法?

android - SDL2 - 检测 "tablet"与 "phone"或屏幕尺寸

ios滚动过一个大的uiview内存问题

ios - 尝试使用 Xcode 4.2 iOS 5 从 UIWebView 推送到详细 View

iphone - 在AppDelegate中获取RootView的实例

ios - 是否可以获得小数点未知的浮点值?

iphone - Reactive Cocoa reduce block 永远不会被调用

ios - 文件是为存档构建的,不是被链接的体系结构 (i386)