在应用内购买后启用被阻止的行。编码问题

Enable blocked rows after in app purchase. Coding issue

我有一个应用程序阻止用户访问视图控制器的几行。这是通过检查 bool 类型的变量是否设置为 true 或 false 来完成的。

var unlocked: bool = false

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell!

    //blocking cells if they are not paid for.
    if unlocked == false {

        if ( indexPath.row  >= 2 ) {
            cell.userInteractionEnabled = false
            cell.contentView.alpha = 0.5
        }
        else{
            cell.userInteractionEnabled = true
            cell.contentView.alpha = 1
        }
    }
    return cell  
}

这非常有效。然后,我可以选择让用户购买对剩余行的访问权限,从而购买应用程序的剩余内容。购买应用内购买后,它将 运行 功能 "updateSections()"。我知道这个功能是在购买时调用的,因为我已经测试过了。

我现在想允许用户从 "updatedSections()" 函数访问 table 视图中的剩余行,因为他们会为此付费。

我试过的是:

//function to unlock
func unlockSections() {

    //This is the code for what happens once the device has bought the IAP. going to have to save what happens here in using nsuserdefaults to make sure it will work when the app opens and closes.

    print("The IAP worked")
    let unlocked = true
    tableview.reloadData()
}

但这似乎不起作用。我看不出哪里错了。

问题是这一行:

let unlocked = true

正在定义一个名为 unlocked 的新常量,它只存在于您的 unlockSections 方法的范围内。它与 class 开头定义的名为 unlocked 的 属性 完全不同。要更新 属性 而不是创建新常量,只需删除 "let":

unlocked = true

或者如果你想 crystal 清楚(或者你想两者兼而有之,但在特定情况下使用 属性),请使用 "self." 来强调你是打算使用 属性:

self.unlocked = true