ios - 如何通过应用内购买锁定表格 View 中的特定行?

标签 ios swift xcode tableview storekit

我有一个表格 View ,要求用户付费才能访问其内容。然而,整个 TableView 被锁定。例如,我希望前两行解锁,第三行锁定。我还有其他超过 12 行的 TableView ,但现在只是发布这个 View Controller 。我通过数组输入数据,并且已经设置了应用内购买。这是我当前的代码:

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)

}

}

最佳答案

我用以下代码修复了它:

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)

}

关于ios - 如何通过应用内购买锁定表格 View 中的特定行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54607146/

相关文章:

ios - iOS 中各种条码阅读器免费 SDK

c++ - 在多个编译单元中使用的 C++ 库导致链接器错误

ios - 如何快速静音 MPMoviePlayerController

ios - 在 Swift 中,我应该在完成后将可选实例变量设置为 nil 吗?

swift - 在 Swift 中创建带有填充底部的按钮图像

swift - 使用AVPlayer返回 “non-Multipath connection”错误

ios - 核心蓝牙和 MFi

ios - swift 4 中的完成处理程序

ios - 使用键盘时使文本字段向上滑动 swift 5

ios - UITableView 导航后滚动到选定的单元格