我正在构建一个待办事项列表应用程序,我希望能够编辑表中的条目并将其替换为新条目。我即将能够做我想做的事,但不会放弃。这是到目前为止我的代码:
/*
IBOutlet NSTextField *textField;
IBOutlet NSTabView *tableView;
IBOutlet NSButton *button;
NSMutableArray *myArray;
*/
#import "AppController.h"
@implementation AppController
-(IBAction)addNewItem:(id)sender
{
[myArray addObject:[textField stringValue]];
[tableView reloadData];
}
- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [myArray count];
}
- (id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex
{
return [myArray objectAtIndex:rowIndex];
}
- (id)init
{
[super init];
myArray = [[NSMutableArray alloc] init];
return self;
}
-(IBAction)removeItem:(id)sender
{
NSLog(@"This is the index of the selected row: %d",[tableView selectedRow]);
NSLog(@"the clicked row is %d",[tableView clickedRow]);
[myArray replaceObjectAtIndex:[tableView selectedRow] withObject:[textField stringValue]];
[myArray addObject:[textField stringValue]];
//[tableView reloadData];
}
@end
最佳答案
尚不清楚您遇到了什么问题,因此这里有一个更好的方法来实现编辑:
为什么不只是 have your data source respond to tableView:setObjectValue:forTableColumn:row:
messages消息?然后,用户可以通过双击表格 View 中的值直接编辑这些值;不需要单独的文本字段。
还有a delegate method you can implement如果您只想允许编辑某些列而不编辑其他列。
关于cocoa - 如何编辑 NSTableView 中的一行以允许删除该行中的数据并替换为新数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2769964/