2016-04-06 8 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':

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 } } 

このようなあなたのセル項目に固有のものを作成しますか? –

+0

@NicolasMiari私は、OSXアプリケーションを開発する際にテーブルビューのセルで同じ問題を抱えています。 – yesthisisjoe

+0

あなたは 'として'を使ってみましたか? –

答えて

0

これを達成するためにジェネリックを使用できます。ジェネリックArrayDataSourceを作成します。

let postArrayDataSource = ArrayDataSource<postTableCell, Post>(items: posts, cellIdentifier: "tableCell", configureCellBlock: configurePostTableViewCell) 
関連する問題