我正在我的应用中创建一个消息页面,我遇到了一些麻烦。当页面首次加载时它看起来很好但是当你开始向下滚动时,一切都搞砸了。新单元格没有加载,然后当我向后滚动时,大多数单元格都消失了,除了通常显示的单元格,它位于错误的位置。
正在进行一些异步操作。发送消息时,它会发送到服务器,然后当它返回时,我重新加载数据并滚动到底部新输入的消息。这也是非常错误的。大多数情况下,发送消息时新单元格不会显示。然后在发送1或2个之后,将全部显示出来。
这是我的ViewController,它也是我的数据源和代理
import UIKit
class MessageDetailController: UIViewController,UITableViewDataSource,UITableViewDelegate, UITextFieldDelegate {
@IBOutlet weak var lblMessageText: UITextField!
@IBOutlet weak var sendViewHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var messagesTable: UITableView!
@IBOutlet weak var sendMsgView: UIView!
@IBOutlet weak var sendMessageView: UIView!
var messageDetailArray = [Message]()
override func viewDidLoad() {
super.viewDidLoad()
self.lblMessageText.delegate = self
self.view.bringSubviewToFront(self.sendMessageView)
self.messagesTable.separatorStyle = UITableViewCellSeparatorStyle.None
self.messagesTable.dataSource = self
self.messagesTable.delegate = self
self.messagesTable.reloadData()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return messageDetailArray.count
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
_ = MessageService().sendMessage("James", message: lblMessageText.text!, callback: loadMessages)
lblMessageText.text = ""
return true
}
func loadMessages(jsonData:NSArray){
dispatch_async(dispatch_get_main_queue(),{
var messages = [Message]()
for messageTest in jsonData {
if let message = messageTest as? NSDictionary {
messages.append(Message(messageType:Message.QA.A, from:message["user_display_label"] as! String, messageText:message["message"] as! String, timeSent:NSDate()))
}
}
self.messageDetailArray = messages;
self.messagesTable.reloadData()
self.messagesTable.scrollToRowAtIndexPath(NSIndexPath(forRow: self.messageDetailArray.count - 1, inSection: 0), atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)
})
}
@IBAction func endedEditing(sender: AnyObject) {
self.view.layoutIfNeeded()
UIView.animateWithDuration(0.2, animations: {
self.sendViewHeightConstraint.constant = 40
self.view.layoutIfNeeded()
}, completion: nil)
}
@IBAction func startedEditing(sender: AnyObject) {
self.view.layoutIfNeeded()
UIView.animateWithDuration(0.2, animations: {
self.sendViewHeightConstraint.constant = 295
self.view.layoutIfNeeded()
}, completion: nil)
}
@IBAction func sendMessage(sender: AnyObject) {
_ = MessageService().sendMessage("James", message: lblMessageText.text!, callback: loadMessages)
lblMessageText.text = ""
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let thisMsg = messageDetailArray[indexPath.row]
let cell: MessageDetailATableViewCell = tableView.dequeueReusableCellWithIdentifier("messageCenterA", forIndexPath: indexPath) as! MessageDetailATableViewCell
cell.msgLabel?.text = thisMsg.messageText;
cell.msgLabel.sizeToFit()
cell.msgLabel.numberOfLines = 0
cell.frame = CGRectMake(0,
0,
self.view.bounds.width,
cell.frame.size.height);
let myInsets = UIEdgeInsetsMake(7, 8, 7, 8)
let bubbleImage = UIImage(named: "balloon_read_right")?.resizableImageWithCapInsets(myInsets)
let bubbleImageView = UIImageView(image: bubbleImage)
bubbleImageView.frame = CGRectMake(0, 0, cell.bubbleView.bounds.width, cell.bubbleView.bounds.height)
cell.bubbleView.addSubview(bubbleImageView)
cell.bubbleView.bringSubviewToFront(cell.msgLabel)
return cell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let messageSize:CGSize = PTSMessagingCell.messageSize(messageDetailArray[indexPath.row].messageText)
return messageSize.height + 2 * PTSMessagingCell.textMarginVertical() + 40
}
}
如果你读完了,你会注意到我使用这个课程,所以我会把它包括在内。它只是表格,并没有做太多。
import UIKit
class MessageDetailATableViewCell: UITableViewCell {
@IBOutlet weak var bubbleView: UIView!
@IBOutlet weak var msgImageView: UIImageView!
@IBOutlet weak var msgLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
我已经搞乱了这段代码,试图让它工作,我找到了其他有相同bug的人。任何帮助将不胜感激!
谢谢!
答案 0 :(得分:2)
问题是由您设置单元格的frame
引起的。您将所有单元格的原点设置为(0,0)。这会破坏表格视图的布局。你不应该从外面触摸细胞的框架。
如果要设置单元格的高度,则必须使用UITableViewDelegate
方法tableView(_:heightForRowAtIndexPath:)
进行设置。计算那里的单元格高度并将其返回。
您不必设置单元格的宽度,因为在UITableView
中,UITableViewCells
的宽度始终与UITableView
相同。