【问题标题】:Adding cell to Table view when button tapped in swift快速点击按钮时将单元格添加到表格视图
【发布时间】:2020-12-12 12:07:04
【问题描述】:

我必须在我的应用中查看。 mainMeasurements 和 historyMode。在主测量中,用户可以拍照并测量距离、速度和时间。我需要该应用程序将在历史模式中的 TableView 中保存测量数据,当点击重置按钮时(如手机应用程序中的最近)。我已经准备好数据并发表了评论。它应该使用图像和“lastTop ...”数据将单元格添加到表格视图并显示旧的

class mainMeasurements: UIViewController, CLLocationManagerDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

var speedArray: [String] = []
var arrayOfImages: [UIImage] = []
var distanceArray: [Int] = []

var lastTopDistance: String?
var lastTopSpeed: String?
var lastTopTime: String?

@IBOutlet weak var takePhoto: UIButton!   
@IBOutlet weak var mountainImage: UIImageView!     
@IBOutlet weak var speedLabel: UILabel!
@IBOutlet weak var distanceLabel: UILabel!
@IBOutlet weak var timerLabel: UILabel!
@IBOutlet weak var resetButtonLabel: UIButton!

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "switchToHistory"{
//            let historyModeVc = segue.destination as! historyMode
//            historyModeVc.memoryPhoto = arrayOfImages.last
//            historyModeVc.historySpeedLabelMM = lastTopSpeed
//            historyModeVc.historyDistanceLabelMM = lastTopDistance
//            historyModeVc.historyTimerLabelMM = lastTopTime
        }
    }

@IBAction func resetDidTouch(_ sender: UIButton) {
        arrayOfImages.append(mountainImage.image!)

        self.lastTopSpeed = self.maxSpeed
        self.lastTopDistance = self.distanceLabel.text
        self.lastTopTime = self.timerLabel.text
        self.arrayOfImages.append(self.mountainImage.image!)
        
    }
 }

import UIKit

class historyMode: UIViewController {
    
    var memoryPhoto: UIImage?
    var historyTimerLabelMM: String?
    var historySpeedLabelMM: String?
    var historyDistanceLabelMM: String?

    override func viewDidLoad() {
        super.viewDidLoad()
//        historyDistanceLabel.text = historyDistanceLabelMM
//        historySpeedLabel.text = historySpeedLabelMM
//        historyTimerLabel.text = historyTimerLabelMM
//        historyImage.image = memoryPhoto
        
    }
   
    

【问题讨论】:

    标签: ios swift uitableview view


    【解决方案1】:

    如果我没记错的话,您是在询问如何在表格视图中显示您的数据?

    您可以像这样解析您的数据并将其传递给historyMode

    struct YourData {
        var arrayOfImages: [UIImage]
        var lastTopDistance: String?
        var lastTopSpeed: String?
        var lastTopTime: String?
    }
    

    创建自定义单元格:

    class CustomCell: UITableViewCell {
        override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
            
            // configure your cell UI
        }
        
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
        func set(with data: YourData) {
            // initialize your properties with the data you provide
        }
    }
    

    使用historyMode 注册您的自定义单元格并将您的数据传递到自定义单元格:

    var yourDataArray: [YourData]!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(CustomCell.self, forCellReuseIdentifier: "CustomCell")
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
        let yourData = yourDataArray[indexPath.row]
        cell.set(with: yourData)
        return cell
    }
    

    【讨论】:

    • 点击按钮时如何将单元格添加到表格视图?
    【解决方案2】:

    向您的数据源添加新元素并使用NotificationCentrehistoryMode 中调用tableView.reloadData()

    【讨论】: