gpt4 book ai didi

ios - reloadData后tableView跳转滚动

转载 作者:搜寻专家 更新时间:2023-10-31 23:01:44 25 4
gpt4 key购买 nike

当我用新数据重新加载 tableView 时,我的 tableView 滚动发生跳跃。以下是 cellForRowAtIndexPath 的代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let section = indexPath.section
let index = indexPath.row
let info = data[sections[section]]![index] as ConversationMessage

if info.from == .Me {
var cell: ConversationDialogMeTableCell = tableView.dequeueReusableCellWithIdentifier("meCell", forIndexPath: indexPath) as! ConversationDialogMeTableCell
cell.backgroundColor = UIColor.clearColor()
cell.selectionStyle = .None
cell.messageLabel.text = info.text
return cell
} else if info.from == .Other {
var cell: ConversationDialogOtherTableCell = tableView.dequeueReusableCellWithIdentifier("otherCell", forIndexPath: indexPath) as! ConversationDialogOtherTableCell
cell.backgroundColor = UIColor.clearColor()
cell.selectionStyle = .None
cell.personName.textColor = UIColor(hex: 0x6d6d6d)
cell.messageContainerView.backgroundColor = UIColor(hex: 0x6d6d6d)
cell.messageContainerView.layer.cornerRadius = 5
cell.messageContainerView.clipsToBounds = true
Alamofire.request(.GET, info.personImage).response {
(request, response, data, error) in
let image = UIImage(data: data!, scale: 1)!
cell.personImage.image = image
cell.personImage.contentMode = UIViewContentMode.ScaleAspectFill
cell.personImage.layer.cornerRadius = cell.personImage.frame.height / 2
cell.personImage.clipsToBounds = true
}
cell.personName.text = info.personName
cell.personMessage.text = info.text
return cell
}
return UITableViewCell()
}

对于第一次加载,滚动是平滑的,但是如果我将在我的 tableView 中添加新数据并调用 reloadData(),则在显示新单元格时滚动会跳跃。

这是我向模型中插入新数据的代码:

func client(client: PubNub!, didReceiveMessage message: PNMessageResult!) {
if message.data.subscribedChannel == self.channel {
if let messageInfo: AnyObject = message.data.message {
let date = (messageInfo["date"] as! String).getDateString()
let messageText = messageInfo["message"] as! String
let from: ConversationMessage.sendFromType = (messageInfo["userID"] as! String) == self.userID ? .Me : .Other
let image = messageInfo["userPhoto"] as! String
let name = messageInfo["userName"] as! String
if data[date] != nil {
data[date]!.append(ConversationMessage(text: messageText, from: from, personImage: image, personName: name, date: messageInfo["date"] as! String))
} else {
data[date] = [ConversationMessage(text: messageText, from: from, personImage: image, personName: name, date: messageInfo["date"] as! String)]
}
for section in self.sections {
self.data[section]! = sorted(self.data[section]!) { Utils.compareDateTime($0.date, with: $1.date, order: .OrderedAscending) }
}
tableView.reloadData()
tableViewScrollToBottom(false)
}
}
}

可能是因为将 tableView 滚动到底部的功能:

func tableViewScrollToBottom(animated: Bool) {
let delay = 0.1 * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))

dispatch_after(time, dispatch_get_main_queue(), {
let numberOfSections = self.tableView.numberOfSections()
let numberOfRows = self.tableView.numberOfRowsInSection(numberOfSections - 1)
if numberOfRows > 0 {
let indexPath = NSIndexPath(forRow: numberOfRows - 1, inSection: (numberOfSections - 1))
self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: animated)
}
})
}

还有一件事。我应该缓存上传的图像吗?也许这是导致跳动的原因。有人可以帮我解决我的问题吗?

最佳答案

不要从

返回 UITableViewAutomaticDimension
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath;

可能有两种可能的解决方案——

  • 要么返回单元格的最大高度。
  • 或者您可以缓存 cell.bounds.size.height 来自tableView:willDisplayCell:forRowAtIndexPath: 并在 estimatedHeightForRowAtIndexPath 中返回相同的值

关于ios - reloadData后tableView跳转滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33284493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com