I have an array of data "electricity" which I am getting from the server. I have a button inside a UITableView Cell name "copyPinButton". Inside 'cellForRowAt indexPath' I am placing the data necessarily. I want to copy a string value that I am getting from the server by pressing 'copyPinButton'. Thanks in advance.
import UIKit
class ElectricityBillTransactionHistoryViewController: BaseViewController {
@IBOutlet weak var electricityTransactionHistoryTableView: UITableView!
private var electricity = [Electricity]()
override func viewDidLoad() {
super.viewDidLoad()
electricityTransactionHistoryTableView.delegate = self
electricityTransactionHistoryTableView.dataSource = self
electricityTransactionHistory()
}
func electricityTransactionHistory() {
webserviceHandler.transactionHIstory(onCompletion: { (TransactionHistoryResponse) in
if TransactionHistoryResponse.code == 200 {
self.electricity = TransactionHistoryResponse.data?.electricity ?? []
self.electricityTransactionHistoryTableView.delegate = self
self.electricityTransactionHistoryTableView.dataSource = self
self.electricityTransactionHistoryTableView.reloadData()
}else {
self.showDialog(title: nil, message: TransactionHistoryResponse.message ?? K.Messages.DefaultErrorMessage, onDefaultActionButtonTap: nil)
}
}, onFailure: { ( _) in
self.showDialog(title: nil, message: K.Messages.DefaultErrorMessage, onDefaultActionButtonTap: nil)
}, shouldShowLoader: true)
}
}
extension ElectricityBillTransactionHistoryViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return electricity.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "electricityTransactionHistoryTableViewCellId", for: indexPath) as! electricityTransactionHistoryTableViewCell
let electricityData = electricity[indexPath.row]
cell.electricityBillHistoryTitleLabel.text = electricityData.packName
cell.electricityBillTransactionIdLabel.text = "\( electricityData.trackerID ?? 0)"
cell.electricityBillAmountLabel.text = "\(electricityData.paidAmount ?? 0)"
cell.electricityBillPaymentDateLabel.text = Util.getFormattedDateString(inputDateString: electricityData.purchasedDate ?? "", inputDateFormat: "yyyy-MM-dd'T'HH:mm:ss.SSSZ", outputDateFormat: "dd MMM yyyy - h:mm a")
return cell
}
}
class electricityTransactionHistoryTableViewCell: UITableViewCell {
@IBOutlet weak var electricityBillContainerView: UIView!
@IBOutlet weak var copyPinButton: UIButton!
@IBOutlet weak var electricityBillImageView: UIImageView!
@IBOutlet weak var electricityBillHistoryTitleLabel: UILabel!
@IBOutlet weak var electricityBillAmountLabel: UILabel!
@IBOutlet weak var electricityBillMonthLabel: UILabel!
@IBOutlet weak var electricityBillTransactionIdLabel: UILabel!
@IBOutlet weak var electricityBillPaymentDateLabel: UILabel!
override func awakeFromNib() {
copyPinButton.layer.masksToBounds = false
copyPinButton.layer.shadowColor = UIColor.gray.cgColor
copyPinButton.layer.shadowOpacity = 0.5
copyPinButton.layer.shadowOffset = CGSize(width: 1.5, height: 0.5)
copyPinButton.layer.shadowRadius = 3
electricityBillContainerView.layer.masksToBounds = false
electricityBillContainerView.layer.shadowColor = UIColor.gray.cgColor
electricityBillContainerView.layer.shadowOpacity = 0.5
electricityBillContainerView.layer.shadowOffset = CGSize(width: 2.0, height: 0.5)
electricityBillContainerView.layer.shadowRadius = 3
}
@IBAction func copyButtonTapped(_ sender: Any) {
}
}
CodePudding user response:
Use UIPasteboard
directly
// write to clipboard
UIPasteboard.general.string = "Hello world"
// read from clipboard
let content = UIPasteboard.general.string
Check this question for more detailed answers
CodePudding user response:
You can copy text or string using UIPasteboard
like below:
@IBAction func copyButtonTapped(_ sender: Any) {
UIPasteboard.general.string = "Your value"
}