在 swift 中点击按钮时将单元格添加到 Table 视图
Adding cell to Table view when button tapped in swift
我必须在我的应用程序中查看。 mainMeasurements 和 historyMode。在主测量中,用户可以拍照并测量距离、速度和时间。我需要该应用程序将在点击重置按钮时以 historyMode 将测量数据保存在 TableView 中(就像 phone 应用程序中的最近)。我已经准备好数据并对其进行了评论。它应该将单元格添加到 table 带有图像和“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
}
如果我没记错的话,您是在询问如何在 table 视图中显示您的数据?
您可以像这样解析您的数据并将其传递给 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
}
向数据源添加新元素并使用 NotificationCentre
在 historyMode
中调用 tableView.reloadData()
我必须在我的应用程序中查看。 mainMeasurements 和 historyMode。在主测量中,用户可以拍照并测量距离、速度和时间。我需要该应用程序将在点击重置按钮时以 historyMode 将测量数据保存在 TableView 中(就像 phone 应用程序中的最近)。我已经准备好数据并对其进行了评论。它应该将单元格添加到 table 带有图像和“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
}
如果我没记错的话,您是在询问如何在 table 视图中显示您的数据?
您可以像这样解析您的数据并将其传递给 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
}
向数据源添加新元素并使用 NotificationCentre
在 historyMode
tableView.reloadData()