所以我不断收到此错误(无法将类型' CustomButton.Type'的值转换为预期参数类型' CustomButton') 它只在这行代码上
cell.productStatus.index = indexPath
cell.productStatus.addTarget(self, action: #selector(self.didTapCellButton(sender: CustomButton)), for: UIControlEvents.touchUpInside)
我无法弄清楚为什么会出现这个问题。 这就是CustomButton的编码方式。
import Foundation
import UIKit
class CustomButton:UIButton{
var index:IndexPath? }
//
// ViewController.swift
//
//
// Created by on 10/15/17.
// Copyright © 2017 Kadin Loehr. All rights reserved.
//
import UIKit
import StoreKit
class ViewController: UIViewController{
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var indicator: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let nib = UINib(nibName: "ProductTableViewCell", bundle: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.SKProductsDidLoadFromiTunes), name: NSNotification.Name.init("SKProductsHaveLoaded"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.StoreManagerDidPurchaseNonConsumable(notification:)), name: NSNotification.Name.init("DidPurchaseNonConsumableProductNotification"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.SKProductsDidLoadFromiTunes), name: NSNotification.Name.init(rawValue: "ReceiptDidUpdated"), object: nil)
SKProductsDidLoadFromiTunes()
}
@objc func StoreManagerDidPurchaseNonConsumable(notification:Notification){
guard let id = notification.userInfo?["id"] else {
return
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
//Since this function already update our UI. Let's use it for our receiptDidUpdated
@objc func SKProductsDidLoadFromiTunes(){
//Now we need to update the table since we have the products ready
//We need to use the main thread when updating the UI
DispatchQueue.main.async {
self.indicator.stopAnimating()
self.tableView.isHidden = false
self.tableView.reloadData()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
//Selector from cell button (Unlock button)
func didTapCellButton(sender:CustomButton){
let index = sender.index
let product = StoreManager.shared.productsFromStore[index!.row]
StoreManager.shared.buy(product: product)
}
}
extension ViewController:UITableViewDelegate,UITableViewDataSource{
// Cell Height
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 165
}
//Number of sections
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
//Number of rows in section
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//Let's feed our table with the number of products
return StoreManager.shared.productsFromStore.count
}
func didTapCellButton(_ customButton: CustomButton) {
print("didTapCellButton")
} //Cell for row
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let product = StoreManager.shared.productsFromStore[indexPath.row]
func didTapCellButton(_ customButton: CustomButton) {
print("didTapCellButton")
}
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ProductTableViewCell
cell.productName.text = product.localizedTitle
cell.productDescription.text = product.localizedDescription
//You should always use NumberFormatter for the price in order to show the correct price currency
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = product.priceLocale
cell.productPrice.text = formatter.string(from: product.price)
cell.productStatus.index = indexPath
cell.productStatus.addTarget(self, action: #selector(didTapCellButton), for: .touchUpInside)
//Let's change the button from Buy to Purchased and change the color as well when the item is already purchased
if StoreManager.shared.isPurchased(id: product.productIdentifier){
cell.productStatus.setTitle("Purchased", for: .normal)
cell.productStatus.setTitleColor(UIColor.green, for: .normal)
}
//Let's show subscribe button instead of buy
if StoreManager.shared.autoSubscriptionsIds.contains(product.productIdentifier){
cell.productStatus.setTitle("Subscribe", for: .normal)
//Let's change the status of the button if the user is subscribed
if StoreManager.shared.receiptManager.isSubscribed{
cell.productStatus.setTitle("Subscribed", for: .normal)
cell.productStatus.setTitleColor(UIColor.green, for: .normal)
}
}
return cell
}
}
根据要求我在这里添加了整个代码
我希望这有效 为什么我不断得到这些问题真的没有意义。每次我尝试更改自定义按钮,使其工作,我得到另一个错误。我之前在Xcode 8中使用过这段代码,但是因为我完成了它所做的所有帮助!!!!!!!
答案 0 :(得分:1)
您无需在#selector
中传递所有这些参数。这就足够了,将摆脱你的错误:
cell.productStatus.addTarget(self, action: #selector(didTapCellButton), for: .touchUpInside)
func didTapCellButton(_ customButton: CustomButton) {
print("didTapCellButton")
}