gpt4 book ai didi

ios - UITableViewCell 子类的行为不像 UITableViewCells

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:01:20 29 4
gpt4 key购买 nike

我的函数接受类型为 (cell: UITableViewCell, item: AnyObject) -> () 的函数作为参数。

我的问题是,当我尝试将具有 UITableViewCell子类 的函数作为参数传递时,出现错误:

无法将类型“(tableCellSubclass, post: Post) -> ()”的值转换为预期的参数类型“(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.")
}
}

最佳答案

您可以使用泛型来实现这一点。创建一个通用的 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)

关于ios - UITableViewCell 子类的行为不像 UITableViewCells,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36463014/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com