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中自定義單元格
什麼它看起來就像當 「添加」:
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.
}
}
}
與自定義單元格關聯類第一次按下
它看起來像什麼時「添加」按下多次
問題與細胞的高度相關的,你需要增加它太。 –