【发布时间】:2015-02-07 02:20:20
【问题描述】:
我两天前就被困在这里,找不到如何管理这个.. 我有一个 uitableview,有一系列自定义单元格和部分,这就是我想要做的:
- 第 1 部分:只有一行,里面有一个标签
- 第 2 部分:日期选择器(为此我使用了 DVDatePickerTableViewCell 类)
这是表格视图的代码
import UIKit
class DettagliRichiestaTVC: UITableViewController {
//sections contiene le sezioni
let sections: NSArray = ["Stato", "Data", "Priorità", "Richiesta", "Risposta"]
//cells contiene tutte le righe della tabella, un 2D array
var cells:NSArray = []
var stato:String = "Completato"
@IBOutlet weak var statoLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// statoLabel.text = stato
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 44
// Cells is a 2D array containing sections and rows.
var cellStato = cellDettagli(style: UITableViewCellStyle.Default, reuseIdentifier: "cellStato")
cellStato.label?.text = "Ciao"
cells = [
[cellStato],
[DVDatePickerTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: nil)]
]
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func selectedStato(segue:UIStoryboardSegue) {
let statoRichiesteTVC = segue.sourceViewController as StatoRichiesteTVC
if let selectedStato = statoRichiesteTVC.selectedStato {
statoLabel.text = selectedStato
stato = selectedStato
}
self.navigationController?.popViewControllerAnimated(true)
}
// MARK: - Table view data source
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
var headerFrame:CGRect = tableView.frame
var title = UILabel(frame: CGRectMake(10, 10, 100, 20))
title.font = UIFont.boldSystemFontOfSize(12.0)
title.text = self.sections.objectAtIndex(section) as? String
title.textColor = UIColor(red: 0.6, green: 0.6, blue: 0.6, alpha: 1)
var headerView:UIView = UIView(frame: CGRectMake(0, 0, headerFrame.size.width, headerFrame.size.height))
headerView.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.8)
headerView.addSubview(title)
return headerView
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
var cell = self.tableView(tableView, cellForRowAtIndexPath: indexPath)
if (cell.isKindOfClass(DVDatePickerTableViewCell)) {
return (cell as DVDatePickerTableViewCell).datePickerHeight()
}
return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return cells.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cells[section].count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return cells[indexPath.section][indexPath.row] as UITableViewCell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var cell = self.tableView(tableView, cellForRowAtIndexPath: indexPath)
if (cell.isKindOfClass(DVDatePickerTableViewCell)) {
var datePickerTableViewCell = cell as DVDatePickerTableViewCell
datePickerTableViewCell.selectedInTableView(tableView)
}
self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//println(segue.identifier)
if segue.identifier == "SavePlayerDetail" {
}
if segue.identifier == "SelezionaStatoRichiesta" {
let statoRichiesteTVC = segue.destinationViewController as StatoRichiesteTVC
statoRichiesteTVC.selectedStato = stato
}
}
}
这是自定义单元格类
import UIKit
class cellDettagli: UITableViewCell {
@IBOutlet weak var label: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
func loadItem(#Label: String) {
label.text = Label
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required init(coder aDecoder: NSCoder) {
//fatalError("init(coder:) has not been implemented")
super.init(coder: aDecoder)
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
如果我设置 cellStato.label?.text = "Ciao" ,它会崩溃说“致命错误:在展开可选值时意外发现 nil”。
我还创建了 .xib 文件并将其分配给 cellDettagli 类。 我总是遇到这个错误。
如何设置此标签的值以及日期选择器行的日期?
谢谢
【问题讨论】:
-
你为什么要创建一个单元格数组?
-
表格由自定义单元格组成,在使用应用程序期间可能会发生变化。另外,我可能需要以同样的方式在其他情节提要中创建不同的相似表格..
-
是的。但这些都不是创建存储单元数组的原因。您应该始终使用 tablviews dequeue 方法使单元格出列。这实际上是您在回答中所做的。顺便说一句,您的回答基本上忽略了我首先质疑的单元格数组。
-
是的,我明白,在我的第二个答案中,我在没有数组的情况下这样做.. 但是.. 这样我需要在加载之前知道每个单元格.. 如果我不知道这些我该怎么办,但只知道它们的类/值,而不知道这些单元格的顺序或存在?
-
这听起来像是在单元格中存储数据。你根本不应该那样做。就顺序而言,唯一重要的是数据。单元格没有任何顺序。事实上,细胞应该被重复使用,因此总是会不断变化。
标签: ios uitableview swift null