gpt4 book ai didi

cocoa - NSTableView:新行中没有文本

转载 作者:行者123 更新时间:2023-12-03 17:42:43 26 4
gpt4 key购买 nike

我编写了一个向 NSTableView 添加新行的方法。它添加了新行,但有空,没有文本。代码如下:

AppDelegate.h

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>
{
IBOutlet NSButton *addDreamButton;
IBOutlet NSButton *removeDreamButton;
IBOutlet NSTextField *dreamField;
IBOutlet NSTableView *dreamTable;
IBOutlet NSMutableArray *dreamlist;
IBOutlet NSTextField *testf;
}

- (IBAction)addDream:(id)sender;
- (IBAction)deleteDream:(id)sender;

@property (assign) IBOutlet NSWindow *window;
@end

AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window = _window;

-(int) numberOfRowsInTableView: (NSTableView *) tableView
{
return [dreamlist count];
}

-(void) dreamTable: (NSTableView *) tableview
setObjectValue:(id)anObject
objectValueForTableColumn: (NSTableColumn *) tablecolumn
row:(int) rowIndex
{
[dreamlist replaceObjectAtIndex:rowIndex withObject:anObject];
}

- (IBAction)addDream:(id)sender
{
NSString* thedream = dreamField.stringValue;
[dreamlist addObject: thedream];
[dreamTable reloadData];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
dreamlist = [[NSMutableArray alloc] init];
}
@end

AppDelegate 与 TableView 绑定(bind)为数据源。

因此,当我按下“添加”按钮时,表中会出现空行(我获取文本的文本字段不为空;我调试并看到 MutableArray 包含正确的字符串)。

最佳答案

您的 TableView 数据源需要实现此方法:

- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
return [dreamlist objectAtIndex:rowIndex];
}

关于cocoa - NSTableView:新行中没有文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8192243/

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