swift - UIImage 未出现在 UITableView 的原型(prototype)单元格中(Swift)

标签 swift uitableview ios8 uiimageview autolayout

我创建了一个包含 10 个单元格的 UITableView,每个单元格都使用自动布局附加了一个 UIImageView enter image description here

该表由代码填充到我的 BackTableViewController 中:

TableArray = ["Home Page", "Membership Card", "Recent News", "Fitness  Schedule", "Training Log", "Pool Lap Calculator", "Martial Arts Schedule", "Pool Schedule", "Comments", "Acknowledgements"]

问题是在运行时选择单元格之前,这些图像都不会出现在表格中。我不知道为什么这是……有人吗?

谢谢

编辑:BackTableViewController

导入 UIKit

类 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

}
}

最佳答案

您只需要具有重用标识符的一个原型(prototype)单元。线索就在名称“原型(prototype)”中。将它们视为所有细胞都基于的蓝图。

在您的 View Controller 类中(如果它是一个 UITableViewContoller,则它应该是一个 UITableViewDelegate),您可以使用此方法指定行数:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return yourArray.count
}

这是负责在表格的右行中显示正确信息的方法。

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
}

希望这对您有所帮助。

关于swift - UIImage 未出现在 UITableView 的原型(prototype)单元格中(Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31885036/

相关文章:

ios - 我怎样才能得到联系ID

iOS 7 uitabbar 显示,在 ios8 上不可见

ios - 如何获得 UIWebView 的 iOS 8 系统字体的等效字体?

ios - WKWebview Swift 4 UIProgressBar 不工作 Xcode 9

ios - 如何在同一个 ViewController 中初始化 NSObject 的两个实例 - Swift

swift - 生成对象时的 EXC_BAD_INSTRUCTION

ios - fatal error : Can't unwrap Optional. 无自定义表格单元格

objective-c - 将节标题添加到 UITableView

ios - 选择 DatePicker 后更新单元格中的日期(时间)

ios - UITableViewCell - 拖动整行来移动?