我的函数接受类型为(cell: UITableViewCell, item: AnyObject) -> ()
的函数作为参数。
我的问题是,当我尝试传递一个具有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 :(得分: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)