Ios 自动调整单元格大小不在Swift中工作
我没有添加任何原型单元,但它应该根据最新的iOS 8 tableView工作。这是我的密码Ios 自动调整单元格大小不在Swift中工作,ios,uitableview,swift,Ios,Uitableview,Swift,我没有添加任何原型单元,但它应该根据最新的iOS 8 tableView工作。这是我的密码 class ViewController: UIViewController,UITableViewDelegate { @IBOutlet weak var tableView: UITableView! var tabledata = ["hn, helooo dnsjakdnksandksajndksandkasjnkc sacjkasc jkas kjdjknasjkdnsaklmdlksamxk
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.
}
}
删除/添加这些行时,我看不到任何更改
self.tableView.estimatedRowHeight = 100.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;
它将有助于动态指定表视图行高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *dictItinararay=[arrCodes objectAtIndex:indexPath.row];
CGSize sizeOfName = [[dictItinararay valueForKey:@"Name"] sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(180, 1000) lineBreakMode:NSLineBreakByWordWrapping];
if(sizeOfName.height>45) {
return sizeOfName.height+10;
}
else {
return 45;
}
}
要使自动调整单元大小起作用,必须向单元子视图添加布局约束,以完全描述视图的垂直大小。如果没有这些限制,你的细胞就无法知道它们实际需要多大。这在故事板中最容易实现
estimatedRowHeight
只是对表格视图的一个提示,它通过将单元格的几何计算推迟到滚动时间来增加表格加载时间(autolayout的计算成本可能会很高)。仍然需要自动布局来告诉表视图每个单元格的大小
另外值得注意的是,您没有重用表视图中的单元格。在您的表视图(u:cellforrowatinedexpath:)
方法中,您应该将单元格出列,而不是每次都创建一个新的单元格:
func tableView(tableView: UITableView, cellForRowAtAindexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
// configure cell...
return cell
}
您可以简单地添加行来设置单元格高度
cell.textLabel.numberOfLines = 20
1:加载项ViewDidLoad
tableView.estimatedRowHeight = 140 // prototype cell height
tableView.rowHeight = UITableViewAutomaticDimension
2:在ViewDidDisplay中重新加载tableview
重要提示:要使UITableViewAutomaticDimension正常工作,必须设置与单元格容器视图相关的所有左、右、下和上约束。UITableViewAutomaticDimension仅在使用autolayout时有效,因此,它将根据您在th IB中设置的约束来确定单元格的高度。请查看以下答案: