带有图像和标签的UITableView行

问题描述 投票:0回答:1

我是一个新的程序员,我想从一个应用程序开始,在那里显示一个带有图像和标签的表格(动态)。我可以用静态表做,但现在我想知道如何使用swift。我正在关注各种教程但是所有人都说一个简单的表格只有文字...

我相信你的帮助会像我一样有用很多......

教程,代码,很好接受......

import UIKit

class FirstViewController: UIViewController,UITableViewDelegate {

    @IBOutlet weak var smilefoto: UIImageView!

    var cellContent = ["Rob", "Kirsten", "Tommy", "Ralphie"]

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

        return cellContent.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "tabella")

        cell.textLabel?.text = cellContent[indexPath.row]
        return cell       
    }
}
ios swift image uitableview label
1个回答
0
投票

您需要做的是创建一个自定义UITableViewCell类,您可以在其中创建imageview和标签以及其他任何内容。

您应该查看本教程:

https://www.youtube.com/watch?v=JG_AMY_gSDQ

当你完成这项工作后,你将可以通过该类访问该单元格中的每个对象:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("ProgramCell", forIndexPath: indexPath) as YourCustomTableViewCell

    cell.yourCellImage = cellContent[indexPath.row].yourImage
    return cell

}

也不要忘记将故事板中的tableView插件链接到此viewController并将DataSource设置为self(继承自UITableViewDataSource)

© www.soinside.com 2019 - 2024. All rights reserved.