UIImage 未出现在 UITableView 的原型单元格中 (Swift)
UIImage not appearing in prototype cells in UITableView (Swift)
我创建了一个包含 10 个单元格的 UITableView,每个单元格都使用自动布局附加了一个 UIImageView
table 由代码填充到我的 BackTableViewController 中:
TableArray = ["Home Page", "Membership Card", "Recent News", "Fitness Schedule", "Training Log", "Pool Lap Calculator", "Martial Arts Schedule", "Pool Schedule", "Comments", "Acknowledgements"]
问题是这些图像中的 none 出现在 table 中,直到 运行 选择了单元格。我不知道为什么这是...有人吗?
谢谢
编辑:BackTableViewController
导入 UIKit
class BackTableViewController: UITableViewController {
var TableArray = [String]()
var ImageArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
TableArray = ["Home Page", "Membership Card", "Recent News", "Fitness Schedule", "Training Log", "Pool Lap Calculator", "Martial Arts Schedule", "Pool Schedule", "Comments", "Acknowledgements"]
ImageArray = ["home-50.png","credit_card-50.png","info-50.png","math-50.png","news-50.png","report_card-50.png","weightlift-50.png"]
// Do any additional setup after loading the view.
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return TableArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier(TableArray[indexPath.row], forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = TableArray[indexPath.row]
return cell
}
}
尝试为图像视图提供 "Center x" 约束而不是前导 space,并且您还应该为其提供宽度和高度约束。
您只需要一个 个具有重用标识符的原型单元。线索就在名字 "prototype" 中。将它们视为所有细胞都基于的蓝图。
在您的视图控制器 class 中(如果它是 UITableViewContoller
,则应该是 UITableViewDelegate
)您使用此方法指定行数:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return yourArray.count
}
这是负责在 table 的右行中显示正确信息的方法。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//We create a cell from our prototype that we gave an identifier to in our storyborad.
let cell = tableView.dequeueReusableCellWithIdentifier("ItemCell") as! UITableViewCell
//Create a single instance of an item from our items array. we use the indexpath.row that was passed into this method as the index for the array.
let item = yourArray[indexPath.row]
//You can go into your array and get the right info out regarding this row by using the indexPath.row
cell.textLabel?.text = item
cell.imageView?.image = UIImage(named: item)
//Finally this method returns the correctly configured cell fro the tableview to display.
return cell
}
希望这对您有所帮助。
您需要在 Attributes Inspector 中使用 Interface Builder 设置您的单元格标识符:
然后在您的 cellForRowAtIndexPath
中,您需要传递您之前设置的单元格标识符,如下所示:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// set the identifier cell, not TableArray[indexPath.row]
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = TableArray[indexPath.row]
return cell
}
希望对你有所帮助。
我创建了一个包含 10 个单元格的 UITableView,每个单元格都使用自动布局附加了一个 UIImageView
table 由代码填充到我的 BackTableViewController 中:
TableArray = ["Home Page", "Membership Card", "Recent News", "Fitness Schedule", "Training Log", "Pool Lap Calculator", "Martial Arts Schedule", "Pool Schedule", "Comments", "Acknowledgements"]
问题是这些图像中的 none 出现在 table 中,直到 运行 选择了单元格。我不知道为什么这是...有人吗?
谢谢
编辑:BackTableViewController
导入 UIKit
class BackTableViewController: UITableViewController {
var TableArray = [String]()
var ImageArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
TableArray = ["Home Page", "Membership Card", "Recent News", "Fitness Schedule", "Training Log", "Pool Lap Calculator", "Martial Arts Schedule", "Pool Schedule", "Comments", "Acknowledgements"]
ImageArray = ["home-50.png","credit_card-50.png","info-50.png","math-50.png","news-50.png","report_card-50.png","weightlift-50.png"]
// Do any additional setup after loading the view.
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return TableArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier(TableArray[indexPath.row], forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = TableArray[indexPath.row]
return cell
}
}
尝试为图像视图提供 "Center x" 约束而不是前导 space,并且您还应该为其提供宽度和高度约束。
您只需要一个 个具有重用标识符的原型单元。线索就在名字 "prototype" 中。将它们视为所有细胞都基于的蓝图。
在您的视图控制器 class 中(如果它是 UITableViewContoller
,则应该是 UITableViewDelegate
)您使用此方法指定行数:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return yourArray.count
}
这是负责在 table 的右行中显示正确信息的方法。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//We create a cell from our prototype that we gave an identifier to in our storyborad.
let cell = tableView.dequeueReusableCellWithIdentifier("ItemCell") as! UITableViewCell
//Create a single instance of an item from our items array. we use the indexpath.row that was passed into this method as the index for the array.
let item = yourArray[indexPath.row]
//You can go into your array and get the right info out regarding this row by using the indexPath.row
cell.textLabel?.text = item
cell.imageView?.image = UIImage(named: item)
//Finally this method returns the correctly configured cell fro the tableview to display.
return cell
}
希望这对您有所帮助。
您需要在 Attributes Inspector 中使用 Interface Builder 设置您的单元格标识符:
然后在您的 cellForRowAtIndexPath
中,您需要传递您之前设置的单元格标识符,如下所示:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// set the identifier cell, not TableArray[indexPath.row]
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = TableArray[indexPath.row]
return cell
}
希望对你有所帮助。