【发布时间】:2016-04-19 01:45:29
【问题描述】:
我正在尝试将图像编码为 UIImage,但我遇到了问题。我不确定如何为每个列表项的每个不同图像编码。我正在构建的应用是一个列表,每个列表项都附有图片。
下面是 MyWhatsit 类:
class MyWhatsit {
var name: String {
didSet {
postDidChangeNotification()
}
}
var location: String {
didSet {
postDidChangeNotification()
}
}
var image: UIImage? {
didSet {
postDidChangeNotification()
}
}
var viewImage: UIImage {
return image ?? UIImage(named: "camera")!
}
init( name: String, position: String = "" ) {
self.name = name
self.location = location
}
func postDidChangeNotification() {
let center = NSNotificationCenter.defaultCenter()
center.postNotificationName(WhatsitDidChangeNotification, object: self)
}
}
下面是表格视图:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return things.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
let thing = things[indexPath.row] as MyWhatsit
cell.textLabel?.text = thing.name
cell.detailTextLabel?.text = thing.location
cell.imageView?.image = thing.viewImage
return cell
}
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
things.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
【问题讨论】:
-
请不要破坏您的帖子。
-
我想删除我的帖子。
-
@DavidSmith 您可以在您的问题上举起版主标志以提醒版主。但破坏它不是解决方案。写下你为什么要在标志内删除你的帖子。
-
@DavidSmith 现在你又破坏了它,一个自动标记已经升起,版主很快就会来查看你的情况。同时,请停止删除您帖子的所有内容。