【问题标题】:How to add images one by one into tableviewcell by clicking addImage button in swift4如何通过单击swift4中的addImage按钮将图像一张一张添加到tableviewcell中
【发布时间】:2019-09-24 07:31:52
【问题描述】:
import UIKit

    class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource{


        @IBOutlet weak var tableView: UITableView!
        @IBOutlet weak var addImage: UIImage!

        let animalNames = ["lion","monky","tiger"]
        let animalImages = [UIImage(named: "lion"), UIImage(named: "monky"), UIImage(named: "tiger")]




        override func viewDidLoad() {
            super.viewDidLoad()
        }

        func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }

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

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "mycell",for:indexPath) as! ImageWithTableViewCell
            cell.image1.image=self.animalImages[indexPath .row]
            cell.label1.text=self.animalNames[indexPath .row]
            return cell
        }

       @IBAction func addingImages(_ sender: UIButton) {

            //what code I have to write here
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }


    }

【问题讨论】:

    标签: swift4


    【解决方案1】:

    如果您希望数组可变,请将其声明为 var 而不是 let。并创建 1 个 var numberOfRecordtoShow

    var animalNames = ["lion","monky","tiger"]
    var animalImages = [UIImage(named: "lion"), UIImage(named: "monky"), UIImage(named: "tiger")]
    var numberOfRecordtoShow = 1
    

    将numberOfRecordtoShow 增加 1 直到数组计数。

    @IBAction func addingImages(_ sender: UIButton) {
       if numberOfRecordtoShow > animalNames.count{
          return
       }
    
       numberOfRecordtoShow = numberOfRecordtoShow + 1
       //reload table now
       tableView.reloadData()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return numberOfRecordtoShow
    
    }
    

    【讨论】:

    • 非常感谢兄弟,但我想在单击 addImage 按钮时一张一张地显示图像。通过编写上面的代码,我一次得到了三个我不想要的图像。我希望有机会添加图片。
    • 请尽可能帮助我
    猜你喜欢
    • 1970-01-01
    • 2011-01-29
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多