gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 01:40:01 25 4
gpt4 key购买 nike

我是 iOS 编程的新手,目前我正在尝试使用导航栏中的添加按钮向我的表格 View 添加新行。表中的每个单元格都是带有 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.
}
}
}

这也是我的自定义 Cell 的代码

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com