【发布时间】:2020-11-23 03:52:44
【问题描述】:
我正在从 Twitter API 中提取数据,并将我的表格单元格设置为包含 UITextView 以显示推文。我遇到了一个问题,有时文本在换行之前只会占用一小部分可用空间,如下图所示:
但是如果我返回导航控制器,然后重新加载它应该正常工作的数据:
谁能解释为什么会这样?这是我的表格视图的设置方式:
func numberOfSections(in tableView: UITableView) -> Int {
if let tweets = timeline?.tweets {
return 1
}
return 0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if timeline == nil {
return 0
} else {
if let _timeline = timeline {
return _timeline.tweets!.count
}
}
return 1
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.TweetTableViewCell, for: indexPath) as! TweetTableViewCell
if let _timeline = timeline {
if let tweet = _timeline.tweets?[indexPath.row] {
cell.tweet = tweet
cell.tweetTextView.translatesAutoresizingMaskIntoConstraints = true
cell.tweetTextView.sizeToFit()
cell.tweetTextView.isScrollEnabled = false
}
}
return cell
}
这是我的 TableViewCell:
class TweetTableViewCell: UITableViewCell {
@IBOutlet weak var profileImageView: UIImageView!
@IBOutlet weak var profileNameLabel: UILabel!
@IBOutlet weak var profileTwitterUsername: UILabel!
@IBOutlet weak var tweetTextView: UITextView!
@IBOutlet weak var tweetDateLabel: UILabel!
@IBOutlet weak var tweetTimeLabel: UILabel!
@IBOutlet weak var mediaImageView: UIImageView!
@IBOutlet weak var likesCountLabel: UILabel!
var tweet: Tweet? {
didSet {
self.updateUI()
}
}
func updateUI() {
if let tweet = tweet {
mediaImageView.isHidden = true
let profileImageOriginalSizeUrl = tweet.user.profile_image_url_https.replacingOccurrences(of: "_normal", with: "")
if let profileImageOriginalSizeUrl = URL(string: profileImageOriginalSizeUrl) {
profileImageView.sd_setImage(with: profileImageOriginalSizeUrl, completed: nil) // Get image from cache
}
roundProfileImage()
profileNameLabel.text = tweet.user.name
profileTwitterUsername.text = "@\(tweet.user.screen_name)"
tweetTextView.textContainerInset = UIEdgeInsets.zero
tweetTextView.text = tweet.full_text
tweetDateLabel.text = truncateDate(withDate: tweet.created_at)
tweetTimeLabel.text = truncateTime(withDate: tweet.created_at)
}
if let media = tweet?.entities?.media?[0].media_url_https {
mediaImageView.isHidden = false
if let mediaImageUrl = URL(string: media) {
mediaImageView.sd_setImage(with: mediaImageUrl, completed: nil)
}
}
}
如果有帮助,这里是我的故事板,其中包含包含 UITextView 和 UIImageView 的堆栈视图以及约束:
应用程序运行时,控制台会显示其中一些警告:
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x7b140004a6a0 h=--& v=--& UITextView:0x7b740006d600'I'm back with a brand new...'.height == 95.5 (active)>",
"<NSLayoutConstraint:0x7b1400045ba0 UITextView:0x7b740006d600'I'm back with a brand new...'.height == 100 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7b1400045ba0 UITextView:0x7b740006d600'I'm back with a brand new...'.height == 100 (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2020-08-03 04:57:43.373999+0100 StarChallenge[22459:10425072] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x7b140004a5b0 h=--& v=--& UITextView:0x7b740006d600'I'm back with a brand new...'.minX == 0 (active, names: '|':UIStackView:0x7b4c00045c80 )>",
"<NSAutoresizingMaskLayoutConstraint:0x7b140004a600 h=--& v=--& UITextView:0x7b740006d600'I'm back with a brand new...'.width == 348.5 (active)>",
"<NSLayoutConstraint:0x7b1400046e60 H:[UITextView:0x7b740006d600'I'm back with a brand new...']-(30)-| (active, names: '|':UIStackView:0x7b4c00045c80 )>",
"<NSLayoutConstraint:0x7b1400047040 H:|-(10)-[UIStackView:0x7b4c00045c80] (active, names: '|':UITableViewCellContentView:0x7b4c00045ac0 )>",
"<NSLayoutConstraint:0x7b1400047090 H:[UIStackView:0x7b4c00045c80]-(10)-| (active, names: '|':UITableViewCellContentView:0x7b4c00045ac0 )>",
"<NSLayoutConstraint:0x7b140004ae70 'fittingSizeHTarget' UITableViewCellContentView:0x7b4c00045ac0.width == 375 (active)>"
)
【问题讨论】:
-
视图调试器告诉你什么?有感叹号吗?
-
我已经编辑了我的帖子以包含控制台输出
-
嗯,你的答案有一部分。为什么你说
translates是true对我来说是个谜。但是,您对sizeToFit的使用也是如此。但实际上,请启动 View Debugger。我想你会发现还有更多的问题。
标签: ios swift uitableview autolayout uitextview