在循环遍历数组时返回TableView中的多个不同行

时间:2015-05-19 20:12:41

标签: swift

我想在tableview中填充这么多行,因为我已经定义了条目。在我的实际代码中,我只收到第一项。

我期望将所有3个国家填充。

我的代码如下:

\t

}

谢谢

2 个答案:

答案 0 :(得分:1)

如果您在xxhash之外定义了两个列表,并按以下方式更新cellForRowAtIndexPathnumberOfRowsInSection

cellForRowAtIndexPath

它应该做你想要的。

在上面的代码中只添加了与您的问题相关的代码,您需要其余代码才能工作,我的意思是class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { var countries = ["Turkey","Croatia","USA"] var countryLogos = [UIImage(named: "turkey.png"), UIImage(named: "croatia.png"), UIImage(named: "USA.png") ] // Defines the number of rows in a section table func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ return countries.count } // Defines content of each cell func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! myCell // Populates with the indexPath.row according the position inside the list cell.lbl_countryName.text = countries[indexPath.row] cell.img_countryFlag.image = countryLogos[indexPath.row] return cell } // Rest of code... } 等。您还需要实现两个协议numberOfSectionsInTableViewUITableViewDelegate或者顺便使用故事板定义UITableViewDataSource

我希望这对你有所帮助。

答案 1 :(得分:0)

// Defines the number of rows in a section table
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    return 1
}

您的数据源报告该表只包含一行。