动态表格视图单元格高度swift IOS8
我正在尝试创建tableView,它将根据动态表格视图单元格高度swift IOS8,ios,uitableview,swift,nslayoutconstraint,Ios,Uitableview,Swift,Nslayoutconstraint,我正在尝试创建tableView,它将根据messageLabel自动调整高度。IOS8让这变得容易多了,但我似乎无法做到这一点。到目前为止,我已经创建了tableViewCell自定义类,其中包含一个xib文件,如下所示: 然后设置messageLabel.numberOfLines=0 在viewController中,我在viewDidLoad tableView.estimatedRowHeight = 80.0 tableView.rowHeight = UITableViewAut
messageLabel
自动调整高度。IOS8让这变得容易多了,但我似乎无法做到这一点。到目前为止,我已经创建了tableViewCell自定义类,其中包含一个xib文件,如下所示:
然后设置messageLabel.numberOfLines=0
在viewController
中,我在viewDidLoad
tableView.estimatedRowHeight = 80.0
tableView.rowHeight = UITableViewAutomaticDimension
最后,委托方法
func tableView(tableView:UITableView, numberOfRowsInSection section:Int)->Int
{
return 1
}
func numberOfSectionsInTableView(tableView:UITableView)->Int
{
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell = tableView.dequeueReusableCellWithIdentifier("TwitterPlainCell", forIndexPath: indexPath) as! TwitterPlainCell
let url = NSURL(string: "https://pbs.twimg.com/profile_images/507493048062713856/KaWKfdgW.jpeg")
let data = NSData(contentsOfURL: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check
cell.selectionStyle = UITableViewCellSelectionStyle.None
// cell.backgroundColor = UIColor(rgba: "#f6f7f9")
var name = "Olof Kajbjer"
var userName = "@olofmCS"
var topText = "\(name) \(userName)"
var topCount = count(topText)
var userCount = count(userName)
var userNameLocation = topCount - userCount
cell.profileImage.image = UIImage(data: data!)
cell.dateLabel.text = "55m"
var myMutableString = NSMutableAttributedString(string: topText, attributes: [NSFontAttributeName:UIFont(name: "PT Sans", size: 18.0)!])
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor(rgba: "#A6B0B5"), range: NSRange(location:userNameLocation,length:count(userName)))
myMutableString.addAttribute(NSFontAttributeName, value: UIFont(name: "PT Sans", size: 13)!, range: NSRange(location:userNameLocation,length:count(userName)))
// set label Attribute
cell.topLabel.attributedText = myMutableString
cell.messageLabel.text = "I FUCKING LOST AGAINST @olofmCS ON AIM AWP IN OVERTIME... GGgg"
cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()
return cell
}
结果如下所示,为什么不调整单元格高度
问题与底部约束有关,我必须将其设置为
=
或另一个值单元格的子视图有哪些约束?这可能就是问题所在。有什么特别重要的标签吗?哪个标签是多行标签?你需要确保你有从单元格顶部开始的约束,通过多行视图上方或下方的任何视图,然后向下到单元格底部。顺便说一句,我无法从你的图像中看出单元格没有正确调整其高度,因为我不知道单元格底部在该图像中的位置。所有文本都显示为可见。单元格是否太高?@rdelmar单元格太短,单元格中出现分隔符指示器。据我所知,这实际上可能是一个iOS 8错误。这里经常发生这种事。