我有一个表格视图,要求用户付费才能访问其内容。但是,整个表视图被锁定。例如,我想将前两行解锁,并将第三行锁定。我也有其他的tableview,其中包含超过12行,但现在仅发布此视图控制器。我正在通过数组输入数据,并且已经设置了应用内购买。这是我当前的代码:
import Foundation
import UIKit
class TrappingVC: UIViewController {
@IBOutlet weak var buildingTableView: UITableView!
@IBOutlet weak var settingsButtonItem: UIBarButtonItem!
var trapping: [CellObject] = []
var segueIdentifiers = ["a", "b"]
//VIEWDIDLOAD
override func viewDidLoad() {
super.viewDidLoad()
//LOAD ARRARYS
trapping = createBuildArray()
buildingTableView.delegate = self
buildingTableView.dataSource = self
self.buildingTableView.rowHeight = 100.0
buildingTableView.tableFooterView = UIView()
//CELL SEPARATORS
buildingTableView.layoutMargins = UIEdgeInsets.zero
buildingTableView.separatorInset = UIEdgeInsets.zero
buildingTableView.separatorColor = UIColor.black
buildingTableView.register(UINib.init(nibName: "TrappingCell", bundle: nil), forCellReuseIdentifier: "TrappingCell")
settingsButtonItem.image = UIImage(named: "Settings")
}
@IBAction func showSettingsClicked(_ sender: Any) {
performSegue(withIdentifier: "showSettings", sender: self)
}
//CREATE ARRAY OF BASIC LESSONS
func createBuildArray() -> [CellObject]{
var tempTrapping: [CellObject] = []
let trapping1 = CellObject(image: #imageLiteral(resourceName: "Yellow"), title: "Below")
let trapping2 = CellObject(image: #imageLiteral(resourceName: "Yellow"), title: "Side")
let trapping3 = CellObject(image: #imageLiteral(resourceName: "Yellow"), title: "Above")
tempTrapping.append(trapping1)
tempTrapping.append(trapping2)
tempTrapping.append(trapping3)
return tempTrapping
}
}
//TABLE
extension TrappingVC: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return trapping.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let trappings = trapping[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "TrappingCell") as! TrappingCell
cell.trappingTitle.text = trappings.title
cell.trappingImage.image = trappings.image
if let purchased = UserDefaults.standard.value(forKey: "payment") as? Bool{
if purchased == true{
cell.lockedImage.isHidden = true
}else{
cell.lockedImage.isHidden = false
}
}else{
cell.lockedImage.isHidden = false
}
cell.layoutIfNeeded()
cell.layoutMargins = UIEdgeInsets.zero
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let purchased = UserDefaults.standard.value(forKey: "payment") as? Bool{
if purchased == true{
performSegue(withIdentifier: segueIdentifiers[indexPath.row], sender: self)
}else{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "UnlockContentVC")
self.navigationController?.pushViewController(controller, animated: true)
}
}else{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "UnlockContentVC")
self.navigationController?.pushViewController(controller, animated: true)
}
tableView.deselectRow(at: indexPath, animated: true)
}
}
答案 0 :(得分:0)
我使用以下代码对其进行了修复:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let purchased = UserDefaults.standard.value(forKey: "payment") as? Bool
if indexPath.row < 5 {
performSegue(withIdentifier: segueIdentifiers[indexPath.row], sender: self)
}
else if purchased == true {
performSegue(withIdentifier: segueIdentifiers[indexPath.row], sender: self)
} else {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "UnlockContentVC")
self.navigationController?.pushViewController(controller, animated: true)
}
tableView.deselectRow(at: indexPath, animated: true)
}