2014-12-03 60 views
0

我使用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,它變成可怕:

之前滾動:

enter image description here

滾動後:

enter image description here

任何建議?

在此先感謝

回答

1

在你cellForRowAtIndexPath,正在重用的細胞。但在您的自定義UITableViewCell中,您正在添加(self.addSubViewself.dateself.message多次(yikes),並添加imageContainer的新實例。

您應該在重新添加它們之前清除單元格,或者使用新數據使它們失效,但不要再次執行self.addSubView

+0

謝謝,這很完美! – soling 2014-12-03 18:13:09