【问题标题】:How to add multiple images to to custom table view cell in swift ios?如何在swift ios中将多个图像添加到自定义表格视图单元格?
【发布时间】:2016-12-21 06:00:26
【问题描述】:

我的自定义表格视图单元格中有一个 imageView。我将多张图片复制到 Xcode。
现在我需要在表格视图单元格中显示这些图像。我已经声明了一个 leaderImage 变量,我在其中给出了图像名称。

我怎样才能做到这一点?


这就是我所拥有的:

var leaderImage : [String]!

override func viewDidLoad() {

  self.leadername = ["Oomen Chandy","PC.George","Veena George","M.Mukesh"]
  self.areaName = ["Puthupally","Poonjar","Thrissur","Kollam"]
  self.approvalrate = ["98%","94%","88%","95%"]
    self.leaderImage = ["rahul","Anil","Manu",]

    leadTableSetup()
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

func leadTableSetup(){

    LeadTableView.delegate = self
    LeadTableView.dataSource = self

    self.LeadTableView.register(UINib(nibName: "LeaderBoardTableViewCell", bundle: nil), forCellReuseIdentifier: "leadCell")

}

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

    return leadername.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "leadCell") as! LeaderBoardTableViewCell
    cell.leaderNameLbl.text = leadername[indexPath.row]
    cell.areaLbl.text = areaName[indexPath.row]
    cell.approvalLabel.text = approvalrate[indexPath.row]
    cell.imageView = leaderImage[indexPath.row]

    return cell
}

【问题讨论】:

    标签: ios uitableview uiimageview swift3


    【解决方案1】:

    在单元格中cellForRowAt方法

    cell.imageView.image = UIImage(named: leaderImage[indexPath.row])
    

    在您的 self.leadername 中包含 5 个元素,但您的所有数组的其余部分包含 4 或 3 个元素,这就是您出现问题的原因。

    为了避免崩溃

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

    【讨论】:

    • 我明白了。非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 2011-07-09
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多