无法添加UITableViewDataSource和SKProductsRequestDelegate协议

时间:2015-08-23 15:00:54

标签: ios swift in-app-purchase storekit

我正在尝试将应用内购买添加到我的应用中。但我不知道如何添加应用程序购买。这就是我关注this指南的原因。根据指南,我添加了UITableViewDelegateUITableViewDataSourc e,SKProductsRequestDelegate协议。不幸的是我收到了如下错误。有什么问题,如何解决?

import UIKit
import StoreKit


class IAPurchaceViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, SKProductsRequestDelegate {

Errors

1 个答案:

答案 0 :(得分:0)

您必须实现委托的REQUIRED协议所需的委托方法

我会告诉你,我只是试过这个并且它有效:这里是你需要添加的方法,实际上,这是一个例子,这就是你需要的所有内容:

import Foundation
import UIKit
import StoreKit

class ANExample: UIViewController, UITableViewDataSource, UITableViewDelegate, SKProductsRequestDelegate {


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        return A_CEll //you must code this
    }

    func productsRequest(request: SKProductsRequest!, didReceiveResponse response: SKProductsResponse!) {

    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return A_NUMBER // you must code this!
    }
}

StoreKit的协议就是:

protocol SKProductsRequestDelegate : SKRequestDelegate, NSObjectProtocol {

    // Sent immediately before -requestDidFinish:
    @availability(iOS, introduced=3.0)
    func productsRequest(request: SKProductsRequest!, didReceiveResponse response: SKProductsResponse!)
}

表数据源的协议是:

protocol UITableViewDataSource : NSObjectProtocol {

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int

    // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
    // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

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

    optional func numberOfSectionsInTableView(tableView: UITableView) -> Int // Default is 1 if not implemented

    optional func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? // fixed font style. use custom view (UILabel) if you want something different
    optional func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String?

    // Editing

    // Individual rows can opt out of having the -editing property set for them. If not implemented, all rows are assumed to be editable.
    optional func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool

    // Moving/reordering

    // Allows the reorder accessory view to optionally be shown for a particular row. By default, the reorder control will be shown only if the datasource implements -tableView:moveRowAtIndexPath:toIndexPath:
    optional func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool

    // Index

    optional func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]! // return list of section titles to display in section index view (e.g. "ABCD...Z#")
    optional func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int // tell table which section corresponds to section title/index (e.g. "B",1))

    // Data manipulation - insert and delete support

    // After a row has the minus or plus button invoked (based on the UITableViewCellEditingStyle for the cell), the dataSource must commit the change
    // Not called for edit actions using UITableViewRowAction - the action's handler will be invoked instead
    optional func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)

    // Data manipulation - reorder / moving support

    optional func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath)
}

tableview的协议是:

protocol UITableViewDelegate : NSObjectProtocol, UIScrollViewDelegate {

    // Display customization

    optional func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
    @availability(iOS, introduced=6.0)
    optional func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
    @availability(iOS, introduced=6.0)
    optional func tableView(tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int)
    @availability(iOS, introduced=6.0)
    optional func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
    @availability(iOS, introduced=6.0)
    optional func tableView(tableView: UITableView, didEndDisplayingHeaderView view: UIView, forSection section: Int)
    @availability(iOS, introduced=6.0)
    optional func tableView(tableView: UITableView, didEndDisplayingFooterView view: UIView, forSection section: Int)

    // Variable height support

    optional func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
    optional func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
    optional func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat

    // Use the estimatedHeight methods to quickly calcuate guessed values which will allow for fast load times of the table.
    // If these methods are implemented, the above -tableView:heightForXXX calls will be deferred until views are ready to be displayed, so more expensive logic can be placed there.
    @availability(iOS, introduced=7.0)
    optional func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
    @availability(iOS, introduced=7.0)
    optional func tableView(tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat
    @availability(iOS, introduced=7.0)
    optional func tableView(tableView: UITableView, estimatedHeightForFooterInSection section: Int) -> CGFloat

    // Section header & footer information. Views are preferred over title should you decide to provide both

    optional func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? // custom view for header. will be adjusted to default or specified header height
    optional func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? // custom view for footer. will be adjusted to default or specified footer height

    // Accessories (disclosures). 

    optional func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath)

    // Selection

    // -tableView:shouldHighlightRowAtIndexPath: is called when a touch comes down on a row. 
    // Returning NO to that message halts the selection process and does not cause the currently selected row to lose its selected look while the touch is down.
    @availability(iOS, introduced=6.0)
    optional func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool
    @availability(iOS, introduced=6.0)
    optional func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath)
    @availability(iOS, introduced=6.0)
    optional func tableView(tableView: UITableView, didUnhighlightRowAtIndexPath indexPath: NSIndexPath)

    // Called before the user changes the selection. Return a new indexPath, or nil, to change the proposed selection.
    optional func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath?
    @availability(iOS, introduced=3.0)
    optional func tableView(tableView: UITableView, willDeselectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath?
    // Called after the user changes the selection.
    optional func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    @availability(iOS, introduced=3.0)
    optional func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath)

    // Editing

    // Allows customization of the editingStyle for a particular cell located at 'indexPath'. If not implemented, all editable cells will have UITableViewCellEditingStyleDelete set for them when the table has editing property set to YES.
    optional func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle
    @availability(iOS, introduced=3.0)
    optional func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String!
    @availability(iOS, introduced=8.0)
    optional func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? // supercedes -tableView:titleForDeleteConfirmationButtonForRowAtIndexPath: if return value is non-nil

    // Controls whether the background is indented while editing.  If not implemented, the default is YES.  This is unrelated to the indentation level below.  This method only applies to grouped style table views.
    optional func tableView(tableView: UITableView, shouldIndentWhileEditingRowAtIndexPath indexPath: NSIndexPath) -> Bool

    // The willBegin/didEnd methods are called whenever the 'editing' property is automatically changed by the table (allowing insert/delete/move). This is done by a swipe activating a single row
    optional func tableView(tableView: UITableView, willBeginEditingRowAtIndexPath indexPath: NSIndexPath)
    optional func tableView(tableView: UITableView, didEndEditingRowAtIndexPath indexPath: NSIndexPath)

    // Moving/reordering

    // Allows customization of the target row for a particular row as it is being moved/reordered
    optional func tableView(tableView: UITableView, targetIndexPathForMoveFromRowAtIndexPath sourceIndexPath: NSIndexPath, toProposedIndexPath proposedDestinationIndexPath: NSIndexPath) -> NSIndexPath

    // Indentation

    optional func tableView(tableView: UITableView, indentationLevelForRowAtIndexPath indexPath: NSIndexPath) -> Int // return 'depth' of row for hierarchies

    // Copy/Paste.  All three methods must be implemented by the delegate.

    @availability(iOS, introduced=5.0)
    optional func tableView(tableView: UITableView, shouldShowMenuForRowAtIndexPath indexPath: NSIndexPath) -> Bool
    @availability(iOS, introduced=5.0)
    optional func tableView(tableView: UITableView, canPerformAction action: Selector, forRowAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject) -> Bool
    @availability(iOS, introduced=5.0)
    optional func tableView(tableView: UITableView, performAction action: Selector, forRowAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject!)
}

您会注意到,在上述协议中,您有标记为OPTIONAL的方法,如果您不想要,则无需提供这些方法...但是,您必须在顶部提供OTHER方法每个协议列表都不是可选的。必须使用必需的函数才能使UIViewController符合这些委托。在上面的例子中,我给你的方法是唯一需要的3个,但你可以使用下面的所有方法,你可以选择,但需要3个。

这里有更多的信息,以便你可以了解这些东西,它只是第一次有点复杂,但一旦你接受,这一切都很简单。如果您有其他问题,请随时提出。

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithProtocols/WorkingwithProtocols.html