我使用swift創建一個聊天應用程序。在我的故事板上,我有一個負責顯示消息的視圖控制器。我有3個迅速文件:Swift - 滾動時tableViewCell問題
ChatingViewController:與視圖控制器上的腳本中
CustomChatingTableViewController相關
CellChatingTableViewCell
被顯示在小區內的每個消息類。我以編程方式創建tableView。
- ChatingViewController
import UIKit
class ChatingViewController: UIViewController {
var messageController = [[String:String]]()
var tableViewController = CustomChatingTableViewController()
override func viewDidLoad() {
let sizeTableView = CGSize(width: view.frame.size.width - 2 * margin, height: view.frame.size.height - sizeTextField.height - 2 * margin - self.navigationController!.navigationBar.frame.size.height)
let originTableView = CGPoint(x: margin, y: 2 * margin + self.navigationController!.navigationBar.frame.size.height)
tableViewController.tableView.frame = CGRect(origin: originTableView, size: sizeTableView)
tableViewController.tableView.registerClass(CellChatingTableViewCell.self, forCellReuseIdentifier: "Cell")
tableViewController.data = messageController
tableViewController.tableView.separatorStyle = .None
view.addSubview(tableViewController.tableView)
}
- CustomChatingTableViewController
import UIKit
class CustomChatingTableViewController: UITableViewController {
var data:[[String:String]]!
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as CellChatingTableViewCell
cell.setCell(data[indexPath.row]["name"] as String!, date: data[indexPath.row]["date"] as String!, message: data[indexPath.row]["message"] as String!)
return cell
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 150
}
}
** ** -CellChatingTableViewCell
import UIKit
class CellChatingTableViewCell: UITableViewCell {
var date = UILabel()
var message = UILabel()
func setCell(name:String,date:String,message:String){
let imageContainerMessage = UIImage(named: "orange.png")!.stretchableImageWithLeftCapWidth(24, topCapHeight: 15)
self.date.font = UIFont(name: "Arial", size: 10)
self.date.text = date
self.date.numberOfLines = 0
self.date.lineBreakMode = NSLineBreakMode.ByWordWrapping
let sizeDateLabelMax = CGSizeMake(self.frame.size.width, 9999)
let expectedSizeDate = self.date.sizeThatFits(sizeDateLabelMax)
self.date.frame = CGRect(origin: CGPoint.zeroPoint, size: expectedSizeDate)
self.message.font = UIFont(name: "Arial", size: 15)
self.message.text = message
self.message.numberOfLines = 0
self.message.lineBreakMode = NSLineBreakMode.ByWordWrapping
let sizeMessageLabelMax = CGSizeMake(self.frame.size.width, 9999)
let expectedSizeMessage = self.message.sizeThatFits(sizeMessageLabelMax)
self.message.frame = CGRect(origin: CGPoint(x: 15, y: 10), size: expectedSizeMessage)
var imageContainer = UIImageView(frame: CGRect(origin: CGPoint(x: 0, y: expectedSizeDate.height + 5), size:
CGSizeMake(expectedSizeMessage.width + 25, expectedSizeMessage.height + 25)))
imageContainer.image = imageContainerMessage
self.addSubview(self.date)
self.addSubview(imageContainer)
imageContainer.addSubview(self.message)
}
}
當我加載視圖控制器,一切都沒有工作正常,但當我滾動的tableView,它變成可怕:
之前滾動:
滾動後:
任何建議?
在此先感謝
謝謝,這很完美! – soling 2014-12-03 18:13:09