ios - 如何在 Swift 中向 TableView 添加自定义单元格?

标签 ios iphone uitableview swift

我对 iOS 编程非常陌生,目前正在尝试使用导航栏中的添加按钮向 TableView 添加新行。表中的每个单元格都是带有 UITextfield 的自定义单元格。当我在模拟器中运行程序时,我的第一个单元格显示出来,但是当我尝试添加另一个单元格时,tableView 变成黑屏(但导航栏仍然保留在屏幕上)。请帮忙!

import UIKit

class TrivialDecisionsController: UITableViewController
{

var objects = [listedDecision]()

override func awakeFromNib() {
    super.awakeFromNib()

}

override func viewDidLoad()
{

    super.viewDidLoad()
    self.navigationItem.leftBarButtonItem = self.editButtonItem()

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


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func insertNewObject(sender: AnyObject?)
{
    let newDecision = tableView.dequeueReusableCellWithIdentifier("trivialDecision") as! listedDecision
    newDecision.configure(text: "", placeholder: "Decision Title")
    self.objects.insert(newDecision, atIndex:0)
    let indexPath = NSIndexPath(forRow: 0, inSection: 0)
    self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
    tableView.reloadData()   
}

// MARK: - Table View

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // 1
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return objects.count+1
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("trivialDecision") as! listedDecision

    //cell.configure(text: "", placeholder: "Enter some text!")
    return cell
}

override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
           return true
}

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {
        objects.removeAtIndex(indexPath.row)
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    } else if editingStyle == .Insert {

        objects.append(listedDecision())

       array, and add a new row to the table view.
    }
}
}

这也是我的自定义单元格的代码

import UIKit

class listedDecision: UITableViewCell, UITextFieldDelegate {


@IBOutlet weak var decisionTitle: UITextField!
/*var ID: String
var cellstyle:UITableViewCellStyle*/


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
}

func configure(#text: String?, placeholder: String)
{
    decisionTitle.text = text
    decisionTitle.placeholder = placeholder
    decisionTitle.accessibilityValue = text
    decisionTitle.accessibilityLabel = placeholder
}


}

最佳答案

使用此代码

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

var cell: MyCustomCell = tableView.dequeueReusableCellWithIdentifier("ThemeCell") as MyCustomCell

let theme = themes[indexPath.row]

cell.myLabel.text = theme.themeName
cell.myImageView.image = UIImage(named: "test.png")

println("The loaded image: \(cell.myImageView.image)")

return cell;
}

关于ios - 如何在 Swift 中向 TableView 添加自定义单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30715716/

相关文章:

iphone - iPhone4 的示例视频捕获代码

iphone - UITableView - 突出显示选定的单元格 [iOS]

ios - 在 TableView 中保存右行

ios - 将纬度经度数据从另一个 View Controller 传递到谷歌地图 - Swift

ios - 尝试读取 InfoPlist.strings 文件...获取 key (?)

iphone - 如何正确构建本地电影URL?

iphone - 找不到协议(protocol)声明 MKAnnotation

ios - UITableView 数据显示在模拟器中,但不显示在实际设备上

iphone - 有什么类似于 XNA for Android 或 IOS 的吗?

ios - 一部 Iphone 6 与另一部的布局差异