Reputation: 39
I have a tableView with an image and a textField but they are not showing the data. What could be missing?
I have tableColumn identifier set as MainCell... I converted obj-c from here to swift.
Any suggestions?
import Cocoa
class viewTime: NSViewController, NSTableViewDataSource, NSTableViewDelegate {
var tableContents:NSMutableArray = []
@IBOutlet var tableView: NSTableView!
override func viewDidLoad() {
super.viewDidLoad()
let path: NSString = "/Library/Application Support/Apple/iChat Icons/Flags"
let fileManager:FileManager = FileManager.default
let directoryEnum:FileManager.DirectoryEnumerator = fileManager.enumerator(atPath: path as String)!
while let file = directoryEnum.nextObject() as? NSString{
let filePath:NSString = path.appendingFormat("/%@", file)
let obj:NSDictionary = ["image": NSImage(byReferencingFile:filePath as String), "name": file.deletingPathExtension]
print("obj:\(obj)")
tableContents.add(obj)
}
self.tableView.reloadData()
}
func numberOfRows(in tableView: NSTableView) -> Int {
return tableContents.count
}
private func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView! {
let cellView = tableView.make(withIdentifier: "MainCell", owner: self) as! NSTableCellView
let flag:NSDictionary = tableContents[row] as! NSDictionary
let identifier:NSString = tableColumn!.identifier as NSString
if (identifier == "MainCell") {
cellView.textField?.stringValue = flag["name"] as! String
cellView.imageView!.image = flag["image"] as? NSImage
return cellView
}
return nil
}
}
Upvotes: 0
Views: 640
Reputation: 285059
Translating Objective-C to Swift literally is always a bad idea.
datasource
and delegate
of the table view are connected to the view controller in Interface Builder.NSTableCellView
is MainCell
Most important change: Create a custom struct, it makes things so much easier.
struct Asset {
var name : String
var image : NSImage
}
Declare the data source array as Swift Array of the struct type.
var tableContents = [Asset]()
This is viewDidLoad
in real Swift code. Don't annotate types unless the compiler tells you to do.
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(fileURLWithPath:"/Library/Application Support/Apple/iChat Icons/Flags")
let enumerator = FileManager.default.enumerator(at: url, includingPropertiesForKeys: [], options: .skipsHiddenFiles, errorHandler: nil)!
for case let fileURL as URL in enumerator {
let asset = Asset(name: fileURL.deletingPathExtension().lastPathComponent, image: NSImage(contentsOf:fileURL)!)
tableContents.append(asset)
}
self.tableView.reloadData()
}
numberOfRows
is OK.
tableView:viewForColumn:Row
can be simplified as there is only one identifier and one column.
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
let cellView = tableView.make(withIdentifier: "MainCell", owner: self) as! NSTableCellView
let asset = tableContents[row]
cellView.textField!.stringValue = asset.name
cellView.imageView!.image = asset.image
return cellView
}
Upvotes: 2