Swift中基于视图的NSTableView - 如何使用

Alb*_*ini 6 cell tableview swift

我有一个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;
}
Run Code Online (Sandbox Code Playgroud)

这是我在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
}
Run Code Online (Sandbox Code Playgroud)

这是结果:(图像右侧的表格)

请注意,评论//setup cell without force unwrapping it没有意义,我忘了删除它.

代码+结果 TableVie设置 Delegate和DataSource已连接

我错过了什么?

编辑:我甚至试过以下但没有成功:

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;
}
Run Code Online (Sandbox Code Playgroud)

谢谢你们.阿尔贝托

Alb*_*ini 11

经过几个小时的搜索,我发现这种方法有效!

func tableView(tableView: NSTableView, viewForTableColumn: NSTableColumn, row: Int) -> NSView
{
    var cell = tableView.makeViewWithIdentifier("List", owner: self) as NSTableCellView
    cell.textField.stringValue = "Hey, this is a cell"
    return cell;
}
Run Code Online (Sandbox Code Playgroud)