Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在Swift中将自定义单元格添加到TableView?_Ios_Iphone_Uitableview_Swift - Fatal编程技术网

Ios 如何在Swift中将自定义单元格添加到TableView?

Ios 如何在Swift中将自定义单元格添加到TableView?,ios,iphone,uitableview,swift,Ios,Iphone,Uitableview,Swift,我对iOS编程非常陌生,目前正在尝试使用导航栏中的add按钮将新行添加到表视图中。表中的每个单元格都是带有UITextfield的自定义单元格。当我在模拟器中运行我的程序时,我的第一个单元格出现了,但当我尝试添加另一个单元格时,tableView变成了一个黑屏(但导航栏仍保留在屏幕上)。请帮忙 import UIKit class TrivialDecisionsController: UITableViewController { var objects = [listedDecision

我对iOS编程非常陌生,目前正在尝试使用导航栏中的add按钮将新行添加到表视图中。表中的每个单元格都是带有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
}


}

在代码中,行:

 self.objects.insert(newDecision, atIndex:0)
 let indexPath = NSIndexPath(forRow: 0, inSection: 0)
取常数零。因此,每次单击按钮添加新单元格时,它都会添加到第0行。因此,将其设置为一个变量,并在每次调用insertNewObject

时递增它。使用以下代码

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;
}