Swift:自定义TableView以保存多列数据

时间:2016-06-03 20:56:41

标签: ios iphone swift uitableview

所以我正在自定义一个tableview来保存多个列。我想要3列,并且我正在自定义TableViewCell,除了我遇到障碍。

现在我有一个ViewView中的TableView,TableView准确地保存了一列数据。在这里,我将其更改为三列,我得到一个关于展开可选nil值的错误。

这是viewcontroller与tableview(FinishTestController.swift)的重要部分:

    var bestRank: [String] = ["1", "2", "3", "4", "5"]
var bestScore: [String] = ["-----", "-----", "-----", "-----", "-----"]
var bestTime: [String] = ["-----", "-----", "-----", "-----", "-----"]
override func viewDidLoad() {
    super.viewDidLoad()

    addhighscore()
    loadhighscores()

    self.tableView.registerClass(TableViewCell.self, forCellReuseIdentifier: "cell")
}

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

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return bestRank.count;;
}

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

    cell.column1.text = self.bestRank[indexPath.row]//<-- ERROR points here
    cell.column2.text = self.bestScore[indexPath.row]
    cell.column2.text = self.bestTime[indexPath.row]

    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    println("You selected cell #\(indexPath.row)!")
}

func loadhighscores(){
    var result = db.query("SELECT * from EASY_MATH5 ORDER BY Score DESC, Time ASC LIMIT 5", parameters: nil)
    println("===============================")

    for row in result
    {
        bestScore[i] = row["Score"]!.asString()
        print(bestScore[i])
        bestTime[i] = row["Time"]!.asString()
        println(bestTime[i])

        i++
    }
}

这是我的手机:

class TableViewCell: UITableViewCell {

@IBOutlet weak var column1: UILabel!
@IBOutlet weak var column2: UILabel!
@IBOutlet weak var column3: UILabel!


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
}

}

这是我得到的错误:

致命错误:在展开Optional值时意外发现nil (lldb)

并指向“cell.column1.text = self.bestRank [indexPath.row]”的行,其中包含“Thread 1:EXC_BAD_INSTRUCTION”行。

关于如何解决的任何想法?

3 个答案:

答案 0 :(得分:2)

删除

self.tableView.registerClass(TableViewCell.self, forCellReuseIdentifier: "cell")

viewDidLoad()开始,如果您使用原型单元格,则无需注册UITableViewCell子类。

答案 1 :(得分:0)

使用您的代码我做了一些小调整,我已经中注明了“// nb:” - 它现在运行正常 - 一旦这些小点是除去。

我所做的代码中只有其他变化是使用“Cell1”而不是“cell”和自定义单元格的名称作为“CustomTableViewCell”而不是“TableViewCell”,但这只是出于个人习惯。

  

类ViewController:UIViewController,UITableViewDataSource,UITableViewDelegate {

@IBOutlet var tableView: UITableView!

var bestRank : [String] = ["1", "2", "3", "4", "5"]
var bestScore: [String] = ["-----", "-----", "-----", "-----", "-----"]
var bestTime: [String] = ["-----", "-----", "-----", "-----", "-----"]

// --------------------------------------
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.bestRank.count  // nb: use of "self." and no ";;" at end
}

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

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell1", forIndexPath: indexPath) as! CustomTableViewCell

    cell.column1.text = self.bestRank[indexPath.row]
    cell.column2.text = self.bestScore[indexPath.row]
    cell.column3.text = self.bestTime[indexPath.row]

    return cell
}

// --------------------------------------
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    print("You selected cell #\(indexPath.row)!")
}
// --------------------------------------

override func viewDidLoad() {
    super.viewDidLoad()

    // nb: Not used: "self.tableView.registerClass(TableViewCell.self, forCellReuseIdentifier: "cell")"

// nb:数据源+委托已经在storyboard中分配了tableview,点击拖放到ViewController中。     }

CustomCell就像你的那样......

class CustomTableViewCell: UITableViewCell {

@IBOutlet var column1: UILabel!

@IBOutlet var column2: UILabel!

@IBOutlet var column3: UILabel!

**结果=无错误** 模拟器显示你想要的表...

enter image description here

答案 2 :(得分:-2)

您应该最有可能使用UICollectionView来处理此类行为。

使用集合视图,您可以更好地控制每个单元格的布局。

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionView_class/