【发布时间】:2017-08-20 04:20:37
【问题描述】:
我有一个动态单元格,两个图像之间有一个标签,每个单元格底部图像下方有两个按钮。标签的大小取决于标签中的行数,可以是任何值。我在cellForRowAt indexPath 中获得了标签的文本。在我的viewDidLoad() 中,我已经通过使用这个设置了一个动态单元格:
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 265
问题:标签超过1行时,单元格的高度大小不变。它只有在两种情况下才有机会获得正确的大小:1)当我刷新表格时。 2)当我向下滚动损坏的单元格不在视图中时,然后当我向上滚动时,损坏的单元格位于正确的位置。
我尝试过的:经过数小时的尝试后,有许多消息来源说 2 个相同的事情:1) 确保所有项目对其所有项目都有限制双方(我这样做了,同样的问题发生了)。 2)唯一的另一个说使用UITableViewAutomaticDimension和estimatedRowHeight(我有)。
我该如何解决这个问题或者我错过了什么?
编辑:在得到它是哪种类型的单元格后,我在cellForRowAt indexPath 中调用标签的文本(我有 3 个,它们都做不同的事情)。代码如下:
func loadNews() {
//start finding followers
let followQuery = PFQuery(className: "Follow")
followQuery.whereKey("follower", equalTo: PFUser.current()?.objectId! ?? String())
followQuery.findObjectsInBackground { (objects, error) in
if error == nil {
self.followArray.removeAll(keepingCapacity: false)
//find users we are following
for object in objects!{
self.followArray.append(object.object(forKey: "following") as! String)
}
self.followArray.append(PFUser.current()?.objectId! ?? String()) //so we can see our own post
//getting related news post
let newsQuery = PFQuery(className: "News")
newsQuery.whereKey("user", containedIn: self.followArray) //find this info from who we're following
newsQuery.limit = 30
newsQuery.addDescendingOrder("createdAt")
newsQuery.findObjectsInBackground(block: { (objects, error) in
if error == nil {
//clean up
self.newsTypeArray.removeAll(keepingCapacity: false)
self.animalArray.removeAll(keepingCapacity: false)
self.newsDateArray.removeAll(keepingCapacity: false)
for object in objects! {
self.newsTypeArray.append(object.value(forKey: "type") as! String) //get what type (animal / human / elements)
self.animalArray.append(object.value(forKey: "id") as! String) //get the object ID that corresponds to different class with its info
self.newsDateArray.append(object.createdAt) //get when posted
}
self.tableView.reloadData()
} else {
print(error?.localizedDescription ?? String())
}
})
} else {
print(error?.localizedDescription ?? String())
}
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let type = newsTypeArray[indexPath.row]
if type == "element" {
//fills cell just like animal one
} else if type == "human" {
//fills cell just like animal one
} else { //its an animal cell
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! AnimalCell
let query = query(className: "Animals")
query.whereKey("objectId", equalTo: animalArray[indexPath.row])
query.limit = 1
query.findObjectsInBackground(block: { (objects, error) in
if error == nil {
for object in objects! {
let caption = (object.object(forKey: "caption") as! String)
cell.captionLabel.text = caption
}
} else {
print(error?.localizedDescription ?? String())
}
})
return cell
}
}
【问题讨论】:
-
你把标签 numberOfLines 改成 0 了吗?
-
标签行数为0 @Imad Ali
-
显示您的自定义单元格
-
请参考here,如果还有疑问可以询问。
-
@fphelp 你能添加你的 cellForRowAt 方法吗?特别是如果您正在执行任何类型的调度或网络调用来获取标签文本?
标签: ios swift xcode uitableview swift3