自我调整细胞不适用于Swift

Ada*_*ung 4 uitableview ios swift

我没有添加任何原型单元格,但它应该根据最新的iOS 8 tableView工作.这是我的代码

class ViewController: UIViewController,UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var tabledata = ["hn, helooo dnsjakdnksandksajndksandkasjnkc sacjkasc jkas kjdjknasjkdnsaklmdlksamxklsamxlksamdklsandk cnsdjdnsklnjnfkdnflasfnlfnkdsnfjkdnfjkdsnjkd njsdkadnaksjdnjkasndsakdnkasdnsalkdn cjkndskasdnsakndksandjksandksajndkj ndsjkadnksalndls;adnklsa;mdas,mcjksanckasdjnklscaskncjks" , "hi i am ishan, helooo dnsjakdnksandksajndksandkasjnkc sacjkasc jkas kjdjknasjkdnsaklmdlksamxklsamxlksamdklsandk cnsdjdnsklnjnfkdnflasfnlfnkdsnfjkdnfjkdsnjkd njsdkadnaksjdnjkasndsakdnkasdnsalkdn cjkndskasdnsakndksandjksandksajndkj ndsjkadnksalndls;adnklsa;mdas,mcjksanckasdjnklscaskncjkssndjkasndjksandkjasndkjasndkjasndkjasndjka ", "a" ]
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return tabledata.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
   let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")

    cell.textLabel.text = self.tabledata[indexPath.row]

    return cell
}
override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.estimatedRowHeight = 100.0;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.reloadData()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}
Run Code Online (Sandbox Code Playgroud)

删除/添加这些行时,我没有看到任何更改

    self.tableView.estimatedRowHeight = 100.0;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
Run Code Online (Sandbox Code Playgroud)

Stu*_*art 6

要使自动单元格大小调整起作用,必须将布局约束添加到完全描述视图垂直大小的单元格子视图中.没有这些限制,你的细胞无法知道它们实际需要多大.这在故事板中最容易完成.

estimatedRowHeight只是表格视图的一个提示,通过推迟单元格的几何计算来滚动时间来增加表格加载时间(自动布局可能很昂贵).仍需要Autolayout来告诉表格查看每个单元格的大小.

另外值得注意的是,您不会在表格视图中重复使用单元格.在您的tableView(_:cellForRowAtIndexPath:)方法中,您应该每次都将单元格出列而不是创建新单元格:

func tableView(tableView: UITableView, cellForRowAtAindexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
    // configure cell...
    return cell
}
Run Code Online (Sandbox Code Playgroud)