2014-07-18 129 views
0

我正在尝试将自定义单元格添加到主 - 细节应用程序。我通过使用界面构建器创建xib来创建自定义单元格。该应用程序加载,我可以使用“添加按钮”添加项目。但是单元格显示不正确,我不知道问题出在哪里。自定义单元格在TableView中显示不正确

感谢您的任何意见!

主视图控制器:

import UIKit 

class FeedTableCell : UITableViewCell{ 

    @IBOutlet var userLabel: UILabel 
    @IBOutlet var hoopLabel: UILabel 
    @IBOutlet var postLabel: UILabel 

    func loadItem(#user: String, hoop: String, post: String) { 
     userLabel.text = user 
     hoopLabel.text = hoop 
     postLabel.text = post 
    } 

} 

在Interface Builder中自定义单元格 Custom cell

什么它看起来就像当 “添加”:

import UIKit 

class MasterViewController: UITableViewController { 

    var objects = NSMutableArray() 


    override func awakeFromNib() { 
     super.awakeFromNib() 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     var nipName=UINib(nibName: "FeedTableCell", bundle:nil) 
     self.tableView.registerNib(nipName, forCellReuseIdentifier: "cell") 

     // Do any additional setup after loading the view, typically from a nib. 
     self.navigationItem.leftBarButtonItem = self.editButtonItem() 

     let addButton = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "insertNewObject:") 
     self.navigationItem.rightBarButtonItem = addButton 
    } 

    func insertNewObject(sender: AnyObject) { 
     if objects == nil { 
      objects = NSMutableArray() 
     } 
     objects.insertObject(NSDate.date(), atIndex: 0) 
     let indexPath = NSIndexPath(forRow: 0, inSection: 0) 
     self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as FeedTableCell 

     cell.loadItem(user:"user1", hoop: "#Yolo", post: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magn") 

     return cell 
    } 

    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
     // Return false if you do not want the specified item to be editable. 
     return true 
    } 

    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
     if editingStyle == .Delete { 
      objects.removeObjectAtIndex(indexPath.row) 
      tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 
     } else if editingStyle == .Insert { 
      // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. 
     } 
    } 
} 

与自定义单元格关联类第一次按下 + klicked first time

它看起来像什么时“添加”按下多次 + klicked 3 times

+0

问题与细胞的高度相关的,你需要增加它太。 –

回答

相关问题