2016-04-06 3 views
2

Meine Funktion übernimmt eine Funktion des Typs (cell: UITableViewCell, item: AnyObject) ->() als Parameter.UITableViewCell-Unterklassen verhalten sich nicht wie UITableViewCells

Mein Problem ist, dass, wenn ich versuche, eine Funktion zu übergeben, die eine Unterklasse von UITableViewCell als Parameter hat, erhalte ich die Fehlermeldung:

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

Wie kann ich die Funktion mit Typ (cell: UITableViewCell, item: AnyObject) ->() ändern so dass Funktionen mit Unterklassen von UITableViewCell entsprechen?

Hier sind die relevanten Code-Schnipsel. Die erste ist ArrayDataSource, die ich instanziieren möchte.

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

Dies ist, wo ich von ArrayDataSource bin instanziiert wird, und wo ich meine Störung erhalte.

override func viewDidLoad() { 

     // ... 

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

     // ... 
} 

Schließlich ist dies die Funktion, die ich als Parameter übergeben möchte.

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

Warum das Tag 'osx'? –

+0

@NicolasMiari Ich habe das gleiche Problem mit Tabellenansicht Zellen bei der Entwicklung einer OSX-App. – yesthisisjoe

+0

Haben Sie versucht, 'as?' Zu verwenden? –

Antwort

0

Sie können Generika verwenden, um dies zu erreichen. Erstellen Sie eine generische 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 
    } 
} 

erstellen spezifisch für Ihr Handy und Artikel wie folgt aus:

let postArrayDataSource = ArrayDataSource<postTableCell, Post>(items: posts, cellIdentifier: "tableCell", configureCellBlock: configurePostTableViewCell) 
Verwandte Themen