无法添加 UITableViewDataSource 和 SKProductsRequestDelegate 协议
Cannot add UITableViewDataSource and SKProductsRequestDelegate protocols
我正在尝试将应用内购买添加到我的应用中。但我不知道如何添加应用程序购买。这就是我遵循 this 指南的原因。根据指南,我添加了 UITableViewDelegate
、UITableViewDataSourc
e、SKProductsRequestDelegate
协议。不幸的是,我收到如下所示的错误。有什么问题,我该如何解决?
import UIKit
import StoreKit
class IAPurchaceViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, SKProductsRequestDelegate {
您必须实现委托的必需协议的必需委托方法
我会告诉你,我刚刚试过这个并且它有效:这是你需要添加的方法,事实上,这是一个例子,这就是你所需要的:
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!)
}
table 数据源的协议是这样的:
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)
}
table视图的协议是这样的:
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!)
}
您会注意到,在上面的协议中,您有标记为可选的方法,如果您不想,则不需要提供这些方法...但是,您必须在以下位置提供其他方法每个不是可选的协议列表的顶部。您必须使用所需的功能,以便您的 UIViewController 符合这些委托。在上面的例子中,我给你的方法是唯一需要的 3 个,但如果你愿意,你可以使用下面的所有方法,这由你选择,但是 3 个是必需的。
这里有更多信息,以便您可以了解这些东西,第一次只是有点复杂,但一旦你掌握了,就很简单了。如果您还有其他问题,请随时提出。
我正在尝试将应用内购买添加到我的应用中。但我不知道如何添加应用程序购买。这就是我遵循 this 指南的原因。根据指南,我添加了 UITableViewDelegate
、UITableViewDataSourc
e、SKProductsRequestDelegate
协议。不幸的是,我收到如下所示的错误。有什么问题,我该如何解决?
import UIKit
import StoreKit
class IAPurchaceViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, SKProductsRequestDelegate {
您必须实现委托的必需协议的必需委托方法
我会告诉你,我刚刚试过这个并且它有效:这是你需要添加的方法,事实上,这是一个例子,这就是你所需要的:
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!)
}
table 数据源的协议是这样的:
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)
}
table视图的协议是这样的:
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!)
}
您会注意到,在上面的协议中,您有标记为可选的方法,如果您不想,则不需要提供这些方法...但是,您必须在以下位置提供其他方法每个不是可选的协议列表的顶部。您必须使用所需的功能,以便您的 UIViewController 符合这些委托。在上面的例子中,我给你的方法是唯一需要的 3 个,但如果你愿意,你可以使用下面的所有方法,这由你选择,但是 3 个是必需的。
这里有更多信息,以便您可以了解这些东西,第一次只是有点复杂,但一旦你掌握了,就很简单了。如果您还有其他问题,请随时提出。