【问题标题】:Swift 3 ViewController with generics type and UITableview具有泛型类型和 UITableview 的 Swift 3 ViewController
【发布时间】:2017-02-02 09:09:50
【问题描述】:

我正在尝试创建一个具有如下泛型类型的 VC:

class SearchViewController<T>: UIViewControlle {
    @IBOutlet weak var tableView: UITableView!
    var delegate: SearchViewControllerDelegate?
    fileprivate var dataArray: [T] = []

...
 }

extension SearchViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return showAddRow ? 1 : dataArray.count

    }
}

但我收到此错误:

非'@objc' 方法'tableView(_:numberOfRowsInSection:)' 没有 满足'@objc'协议'UITableViewDataSource'的要求

我试过这样@objc method

@objc(tableView:numberOfRowsInSection:)

但它没有奏效。我错过了什么?

【问题讨论】:

    标签: ios objective-c swift cocoa-touch generics


    【解决方案1】:

    您需要为 tableView 声明所有协议 ..

     func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int
    {
        return 1
    }
    private func numberOfSectionsInTableView(tableView: UITableView) -> Int
    {
        return 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
    
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
    
    }
    

    【讨论】:

      【解决方案2】:

      如果您查看 Swift 中的 UITableViewDataSource 协议: 你会注意到

      public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
      public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
      

      是非可选方法,因此您必须声明它才能使您的代码正常工作! (与UITableViewDelegate 相同)

      public protocol UITableViewDataSource : NSObjectProtocol {
      
      
          @available(iOS 2.0, *)
          public 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)
      
          @available(iOS 2.0, *)
          public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
      
      
          @available(iOS 2.0, *)
          optional public func numberOfSections(in tableView: UITableView) -> Int // Default is 1 if not implemented
      
      
          @available(iOS 2.0, *)
          optional public func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? // fixed font style. use custom view (UILabel) if you want something different
      
          @available(iOS 2.0, *)
          optional public 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.
          @available(iOS 2.0, *)
          optional public func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> 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:
          @available(iOS 2.0, *)
          optional public func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool
      
      
          // Index
      
          @available(iOS 2.0, *)
          optional public func sectionIndexTitles(for tableView: UITableView) -> [String]? // return list of section titles to display in section index view (e.g. "ABCD...Z#")
      
          @available(iOS 2.0, *)
          optional public func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at 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
          @available(iOS 2.0, *)
          optional public func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
      
      
          // Data manipulation - reorder / moving support
      
          @available(iOS 2.0, *)
          optional public func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-26
        • 1970-01-01
        相关资源
        最近更新 更多