我在我的應用程序中創建了一個消息傳遞頁面,我遇到了一些嚴重的麻煩。第一次加載頁面時看起來不錯,但是當你開始向下滾動時,所有事情都會搞砸。新的單元格不加載,然後當我向後滾動時,大多數單元格都消失了,除非通常顯示爲1,並且顯示在錯誤的位置。表格單元格不在滾動上加載
有一些異步操作正在進行。當郵件發送時,它發送到服務器,然後當它返回時,我重新加載數據並滾動到底部新輸入的消息。這也是非常麻煩的。大多數情況下,發送消息時,新單元不顯示。然後在發送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
}
}
我有這個代碼搞砸周圍很多試圖讓它工作,我找續誰比誰有相同的錯誤。任何幫助將不勝感激!
謝謝!