【发布时间】:2014-08-12 08:07:39
【问题描述】:
我有一个 NSTableView,其单元格是基于视图的。
DataSource 和 Delegate 已连接,但我无法显示单元格的 textField 字符串值。
这是 Objective-C 中的代码,正在运行:
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return 10;
}
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
NSTableCellView *cell = [tableView makeViewWithIdentifier:@"List" owner:self];
[cella.textField setStringValue:"Hey, this is a cell"];
return cell;
}
这是我在 Swift 中的代码,无法正常工作:
func numberOfRowsInTableView(aTableView: NSTableView!) -> Int
{
return 10 //Casual number
}
func tableView(tableView: NSTableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> NSTableCellView! {
var cell = tableView.makeViewWithIdentifier("List", owner: self) as NSTableCellView!
// setup cell without force unwrapping it
cell.textField.stringValue = "Hey, this is a cell"
println("Method called") //Never printed
return cell
}
这是结果:(图片右侧的表格)
注意//setup cell without force unwrapping it的评论没有意义,我忘记删了。
我错过了什么?
编辑:我什至尝试了以下但没有成功:
func numberOfRowsInTableView(aTableView: NSTableView!) -> Int
{
return 10
}
func tableView(tableView: NSTableView!, objectValueForTableColumn tableColumn: NSTableColumn!, row: Int) -> AnyObject
{
var cell = tableView.makeViewWithIdentifier("List", owner: self) as NSTableCellView
cell.textField.stringValue = "Hey this is a cell"
return cell;
}
谢谢大家。 阿尔贝托
【问题讨论】:
-
在 Objective-C 代码中你有
tableView:viewForTableColumn:row:,而在 Swift 中你有tableView(tableView, cellForRowAtIndexPath)。肯定有tableView:viewForTableColumn:row:Swift 等价物?