2016-04-06 32 views
2

我的函數採用(cell: UITableViewCell, item: AnyObject) ->()類型的函數作爲參數。UITableViewCell子類行爲不像UITableViewCells

我的問題是,當我試圖通過具有子類的UITableViewCell作爲參數一個功能,我得到的錯誤:

Cannot convert value of type '(tableCellSubclass, post: Post) ->()' to expected argument type '(cell: UITableViewCell, item: AnyObject) ->()'

我怎樣才能改變函數(cell: UITableViewCell, item: AnyObject) ->()類型所以使用UITableViewCell的子類的函數符合它?

以下是相關的代碼片段。第一個是我嘗試實例化的ArrayDataSource。

class ArrayDataSource: NSObject, UITableViewDataSource { 
    let items: [AnyObject] 
    let cellIdentifier: String 
    let configureCellBlock: (cell: UITableViewCell, item: AnyObject) ->() 

    init(items: [AnyObject], cellIdentifier: String, configureCellBlock: (cell: UITableViewCell, item: AnyObject) ->()) { 
     self.items = items 
     self.cellIdentifier = cellIdentifier 
     self.configureCellBlock = configureCellBlock 
     super.init() 
    } 

    func itemAtIndexPath(indexPath: NSIndexPath) -> AnyObject { 
     return items[indexPath.row] 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return items.count 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) 
     let item = self.itemAtIndexPath(indexPath) 

     configureCellBlock(cell: cell, item: item) 
     return cell 
    } 
} 

這是我從哪裏實例化ArrayDataSource,以及我在哪裏得到我的錯誤。

override func viewDidLoad() { 

     // ... 

     let postsArrayDataSource = ArrayDataSource(items: posts, cellIdentifier: "tableCell", configureCellBlock: configurePostTableViewCell) 

     // ... 
} 

最後,這是我想作爲參數傳遞的函數。

func configurePostTableViewCell(cell: postTableCell, post: Post) { 
     //sets up formatted string for last confirmed 
     let lastConfirmed = dateSimplifier(post.confirmed) 
     var boldLastConfirmed = NSMutableAttributedString() 
     boldLastConfirmed = NSMutableAttributedString(string: "last confirmed: \(lastConfirmed)", attributes: [NSFontAttributeName:UIFont(name: "AvenirNext-Italic", size: 15.0)!]) 
     boldLastConfirmed.addAttribute(NSFontAttributeName, value: UIFont(name: "AvenirNext-DemiBoldItalic", size: 15.0)!, range: NSRange(location: 16, length: lastConfirmed.characters.count)) 

     //sets up formatted string for posted date 
     let posted = dateSimplifier(post.posted) 
     var boldPosted = NSMutableAttributedString() 
     boldPosted = NSMutableAttributedString(string: "posted: \(posted)", attributes: [NSFontAttributeName:UIFont(name: "AvenirNext-Italic", size: 15.0)!]) 
     boldPosted.addAttribute(NSFontAttributeName, value: UIFont(name: "AvenirNext-DemiBoldItalic", size: 15.0)!, range: NSRange(location: 8, length: posted.characters.count)) 

     //sets up formatted string for title & type 
     let title = post.title 
     let type = post.type 
     var boldTitle = NSMutableAttributedString() 
     boldTitle = NSMutableAttributedString(string: "\(title) \(type)", attributes: [NSFontAttributeName:UIFont(name: "AvenirNext-DemiBold", size: 18.0)!]) 
     boldTitle.addAttribute(NSFontAttributeName, value: UIFont(name: "BodoniSvtyTwoSCITCTT-Book", size: 18.0)!, range: NSRange(location: title.characters.count + 2, length: type.characters.count)) 
     if (type == "free") { 
      boldTitle.addAttribute(NSForegroundColorAttributeName, value: UIColor(red: 1.0, green: 0.5, blue: 0.5, alpha: 1.0), range: NSRange(location: title.characters.count + 2, length: type.characters.count)) 
     } else if (type == "cheap") { 
      boldTitle.addAttribute(NSForegroundColorAttributeName, value: UIColor(red: 0.0, green: 0.75, blue: 1.0, alpha: 1.0), range: NSRange(location: title.characters.count + 2, length: type.characters.count)) 
     } 

     //sets values for strings in cells 
     cell.titleLabel.attributedText = boldTitle 
     cell.lastConfirmedLabel.attributedText = boldLastConfirmed 
     cell.postedLabel.attributedText = boldPosted 

     //sets image based on post's status 
     switch post.status { 
     case 0: 
      cell.statusImage.image = UIImage(named: "Help Filled-50.png") 
     case 1: 
      cell.statusImage.image = UIImage(named: "Good Quality Filled-50.png") 
     case 2: 
      cell.statusImage.image = UIImage(named: "Poor Quality Filled-50.png") 
     case 3: 
      cell.statusImage.image = UIImage(named: "Help Filled-50.png") 
     default: 
      NSLog("Unknown status code for post.") 
     } 
    } 
+0

爲什麼標籤'osx'? –

+0

@NicolasMiari我在開發OSX應用程序時遇到了表視單元的問題。 – yesthisisjoe

+0

你嘗試過使用'as?'嗎? –

回答

0

您可以使用泛型來實現此目的。創建一個通用的ArrayDataSource

class ArrayDataSource<T: UITableViewCell, U: AnyObject> : NSObject, UITableViewDataSource { 

    let items: [U] 
    let cellIdentifier: String 
    let configureCellBlock: (cell: T, item: U) ->() 

    init(items: [U], cellIdentifier: String, configureCellBlock: (cell: T, item: U) ->()) { 
     self.items = items 
     self.cellIdentifier = cellIdentifier 
     self.configureCellBlock = configureCellBlock 
     super.init() 
    } 

    func itemAtIndexPath(indexPath: NSIndexPath) -> U { 
     return items[indexPath.row] 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return items.count 
    } 

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

     let cell = tableView.dequeueReusableCellWithIdentifier("identifier", forIndexPath: indexPath) as! T 
     let item = self.itemAtIndexPath(indexPath) 

     configureCellBlock(cell: cell, item: item) 

     return cell 
    } 
} 

創建一個特定於您的細胞和項目是這樣的:

let postArrayDataSource = ArrayDataSource<postTableCell, Post>(items: posts, cellIdentifier: "tableCell", configureCellBlock: configurePostTableViewCell) 
相關問題