tableview错误

时间:2015-04-25 00:06:25

标签: ios uitableview swift

我是Swift和IOS编程的新手,但我正在尝试使用表格视图加载一些数据,但是我收到了一个我从未见过的错误。有人可以帮帮我吗。错误显示在我试图传递我创建的单元格文本标签的行上。错误是"找不到接受提供的参数的下标的重载" enter image description here

import UIKit

class ViewController: UIViewController, UITableViewDelegate {


    var cellContent = [1,2,3]


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }




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

        return cellContent.count
    }



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

        var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")

        cell.textLabel?.text  = cellContent[indexPath.row]
        return cell

    }

3 个答案:

答案 0 :(得分:2)

看起来错误有点误导。这里的实际问题似乎是您尝试将Int设置为String属性。你可以这样做:

cell.textLabel?.text  = String(cellContent[indexPath.row])

通过此更改,您的代码可以正常使用。

答案 1 :(得分:2)

Swift将cellContent视为整数数组。您需要(1)使用cell.textLabel?.text = cellContent[indexPath.row] as String或将数组更改为var cellContent = ["1","2","3"]

答案 2 :(得分:1)

您必须使用字符串作为文本标签。这将解决您的错误:

var cellContent = ["1","2","3"];