【问题标题】:Not able to access the variable declared outside the function in UITableviewcontroller swift无法在 UITableviewcontroller swift 中访问函数外部声明的变量
【发布时间】:2015-11-09 08:35:06
【问题描述】:

我正在使用一个简单的 UITableViewController

var testdata = [String]()

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

    // Configure the cell...
    cell.clabel.text = testdata[indexPath.row]
    return cell
}

testdata 在函数外部声明。 testdata 也是从另一种方法填充的。

问题是:当我放置断点并检查数据时,self.testdata 没有显示任何数据。 我是否在 swift 中正确使用了变量?

在此处添加完整代码:

 import UIKit
 import Alamofire


 class TableViewController: UITableViewController, UITableViewDataSource,             UITableViewDelegate {

struct ListItem {
    var Name:String = ""
    var Number:String = ""
    var Email:String = ""
}


override func viewDidLoad() {
    super.viewDidLoad()
    getjsonData()

    // 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.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
   // return controlstaticdata.count
   // return controlListData.controlListItems.count
    return testdata.count
}

   var testdata = [String]()


    func getjsonData(){

     var jsondata:NSData!

    let url = "http://localhost:3000/controllistapp"

    Alamofire.request(.GET, url).responseJSON(){
        (_,_,data,_) in
        println(data)

    // parse the jsondata returned by Alamofire .GET request

    let json = JSON(data!)
    var i = 0
    for(_, subJson): (String, JSON) in json {
        if let Name = subJson["Name"].string,
            let Number = subJson["Number"].string,
            let Email = subJson["Email"].string{
                var item = ListItem(Name: Name, Number: Number, Email: Email)
                self.testdata += [Name]

        }

    }
}

}

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

    // Configure the cell...
    cell.clabel.text = self.testdata[indexPath.row]
    return cell
} 
}

getjsonData 中 self.testdata += [Name] 行的断点显示正在填充的数据。 但是,tableView 函数中 cell.clabel.text = self.testdata[indexPath.row] 行的断点显示没有数据并引发异常。

【问题讨论】:

  • 请展示您如何填充测试数据
  • 我已经用完整的代码更新了帖子。
  • 您确定在 for 循环中向数组中写入了某些内容吗?否则尝试将数组初始化为var testdata:[String] = []
  • 我已经尝试过 var testdata:[String] = [] now 。结果相同。我可以看到在断点处的调试窗口中填充了测试数据。但是,当我打破 TableView 时,它是空的。

标签: swift uitableview variables scope


【解决方案1】:

我认为您的问题是您在将数据填充到数组后忘记包含 self.tableView.reloadData()

【讨论】:

  • 我已添加数据以测试同一控制器中另一个函数的数据。还是同样的问题。如果它是一个静态数组,那么我可以访问数据。但是对于 var 数组,数据是不可用的。
  • 我想你在将数据放入数组后忘记重新加载tableView..检查编辑答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-22
  • 2021-07-28
  • 2022-01-12
  • 2014-09-22
  • 1970-01-01
  • 1970-01-01
  • 2012-07-07
相关资源
最近更新 更多