Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 是否使用对象属性填充表视图?_Ios_Swift - Fatal编程技术网

Ios 是否使用对象属性填充表视图?

Ios 是否使用对象属性填充表视图?,ios,swift,Ios,Swift,我在我的UITableViewController中创建了一个自定义类,它包含两个属性: import UIKit class FirstTableViewController: UITableViewController { class Continent { var name: String = "Country name" var continentCountries: NSArray = ["countries of selected continent"] }

我在我的
UITableViewController
中创建了一个自定义类,它包含两个属性:

import UIKit

class FirstTableViewController: UITableViewController {

class Continent {

    var name: String = "Country name"
    var continentCountries: NSArray = ["countries of selected continent"]
}
现在,我创建了几个对象并将它们放置在一个数组中:

func setup() { 
var asia = Continent()
    asia.name = "Asia"
    asia.continentCountries = ["Afghanistan","Armenia","Azerbaijan","Bahrain"] //etc..

var africa = Continent()
africa.name = "Africa"
africa.continentCountries = ["Egypt","Sierra Leone","Sudan","South Africa"] //etc..

var northAmerica - Continent() 
northAmerica.name = "North America"
northAmerica.continentCountries = ["Canada","United States of America","Mexico"] 

let theWorld = [asia,africa,northAmerica] 

}
请注意,我必须在“
func setup()
”方法中声明新创建的对象和数组,因为它们未被识别为“大陆”对象,而是“FirstTableViewController”对象

现在,我的目标是用每个对象的第一个属性“name”填充以下表视图,我使用了以下代码:

let aContinent = theWorld[indexPath.row] as! Continent
cell.textLabel.text = aContinent.name
现在的问题是,除了setup()方法之外的任何内容,控制器都无法识别,并标记为“未声明”,因此我无法向
TableViewController的任何协议委托添加代码,即:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

return 1
}

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

return 3 //this is just a temporary Int
}

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

    return cell

应该纠正什么?如何允许新创建的类及其对象与初始的
UITableViewController
类一致,以便填充此表视图?

声明这样的属性,以便可以在委托方法中使用它

class FirstTableViewController: UITableViewController {

class Continent {

    var name: String = "Country name"
    var continentCountries: NSArray = ["countries of selected continent"]
}
let theWorld:[Continent] = {
    var asia = Continent()
    asia.name = "Asia"
    asia.continentCountries = ["Afghanistan","Armenia","Azerbaijan","Bahrain"] //etc..

    var africa = Continent()
    africa.name = "Africa"
    africa.continentCountries = ["Egypt","Sierra Leone","Sudan","South Africa"] //etc..

    var northAmerica = Continent()
    northAmerica.name = "North America"
    northAmerica.continentCountries = ["Canada","United States of America","Mexico"]

     return  [asia,africa,northAmerica]
    }()
 override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1
}

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

    return theWorld.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
    cell.textLabel?.text = theWorld[indexPath.row].name
    return cell
}
}
截图


像这样声明一个属性,以便您可以在委托方法中使用它

class FirstTableViewController: UITableViewController {

class Continent {

    var name: String = "Country name"
    var continentCountries: NSArray = ["countries of selected continent"]
}
let theWorld:[Continent] = {
    var asia = Continent()
    asia.name = "Asia"
    asia.continentCountries = ["Afghanistan","Armenia","Azerbaijan","Bahrain"] //etc..

    var africa = Continent()
    africa.name = "Africa"
    africa.continentCountries = ["Egypt","Sierra Leone","Sudan","South Africa"] //etc..

    var northAmerica = Continent()
    northAmerica.name = "North America"
    northAmerica.continentCountries = ["Canada","United States of America","Mexico"]

     return  [asia,africa,northAmerica]
    }()
 override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1
}

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

    return theWorld.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
    cell.textLabel?.text = theWorld[indexPath.row].name
    return cell
}
}
截图

欢迎来到SO

你看起来有点困惑。您不希望theWorld变量成为设置函数的局部变量。您希望它是FirstTableViewController类的实例变量,并且希望安装程序是该类的实例方法。(在类的大括号内定义。)

欢迎使用SO

你看起来有点困惑。您不希望theWorld变量成为设置函数的局部变量。您希望它是FirstTableViewController类的实例变量,并且希望安装程序是该类的实例方法。(在类的大括号内定义。)

尝试以下操作:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell
    cell.title.text = theWorld[indexPath.row].name
    return cell
}
试试这个:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell
    cell.title.text = theWorld[indexPath.row].name
    return cell
}


我做的和你做的一样,它给了我一个错误,即“在预期返回的闭包中缺少返回”[FirstTableViewController.Continental]“在你添加我的代码后,你能更新问题并发布代码的完整代码吗?我不确定你说的错误在哪里。代码在我的Mac电脑上运行得很好,作为截屏我很抱歉,我的一个var的声明被关闭了。这很有效,谢谢。嘿,我能和你聊天吗?我有一个问题,是你回答了这个问题。”@John Durandi成功地完成了这项工作,正如您所做的那样,它给了我一个错误:“在一个预期返回“[FirstTableViewController.Continental]”的闭包中缺少返回。添加我的代码后,您能否更新问题并发布代码的完整代码。我不确定你所说的错误在哪里。这段代码在我的Mac电脑上运行得很好,因为屏幕截图我很抱歉,我的一个var的声明被关闭了。这很有效,谢谢。嘿,我能和你聊聊吗??我有一个问题,你是成功回答这个问题的人@John Durandi我和你一样做了,这给了我一个错误“在预期返回的关闭中缺少返回”[FirstTableViewController.Continental]“在你添加我的代码后,你能更新问题并发布代码的完整代码吗?我不确定你说的错误在哪里。代码在我的Mac电脑上运行得很好,作为截屏我很抱歉,我的一个var的声明被关闭了。这很有效,谢谢。嘿,我能和你聊天吗?我有一个问题,是你回答了这个问题。”成功地@John durand就是这样,即使我在类内定义了setup方法,当我离开类时,控制器也不会识别我的任何对象。它只会在我在类内工作时识别它们,我不想这样做,因为我想访问委托人。你的描述太模糊了。什么类?什么控制员?你说“当我走出教室”是什么意思?“在我看来,你在处理范围的正常问题。实例变量仅在当前实例的范围内可见,除非您引用所属对象,例如
otherObject.property
。你能编辑你的问题来显示你的方法是在哪里定义的,你的变量是在哪里定义的,以及“控制器不识别我的任何对象”在哪里吗?这就是问题所在,即使我在我的类中定义了setup方法,当我离开类时,控制器也不识别我的任何对象。只有在我在课堂上工作时,它才会识别它们,我不想这样做,因为我想找到被授权人。你的描述太模糊了。什么课?什么控制器?你说的“当我走出教室”是什么意思?我觉得你在处理范围的正常问题。实例变量仅在当前实例的范围内可见,除非您引用所属对象,例如
otherObject.property
。你能编辑你的问题来显示你的方法是在哪里定义的,你的变量是在哪里定义的,以及“控制器不识别我的任何对象”在哪里吗?这就是问题所在,即使我在我的类中定义了setup方法,当我离开类时,控制器也不识别我的任何对象。只有在我在课堂上工作时,它才会识别它们,我不想这样做,因为我想找到被授权人。你的描述太模糊了。什么课?什么控制器?你说的“当我走出教室”是什么意思?我觉得你在处理范围的正常问题。实例变量仅在当前实例的范围内可见,除非您引用所属对象,例如
otherObject.property
。您是否可以编辑您的问题,以显示在哪里定义了方法、在哪里定义了变量以及在哪里“控制器无法识别我的任何对象”?