【问题标题】:How do I copy a NSTableView data using NSPasteBoard?如何使用 NSPasteBoard 复制 NSTableView 数据?
【发布时间】:2019-09-02 20:04:12
【问题描述】:

我正在尝试使用粘贴板复制 NSTableView 数据,并希望将其粘贴为 Microsoft Word 中的表格。我该怎么做?

我尝试将 TableView 数据源复制为字符串,但无法复制 TableView 本身。

    NSPasteboard *pasteboard=[NSPasteboard generalPasteboard];


    //_fillvalue,_levelfill(Mutable array) are the data source for my tableview with 2 column


    [pasteboard clearContents];
    int i=0;
    NSMutableArray *tval=[[NSMutableArray alloc]init];
    NSInteger row=[_tableView numberOfRows]; 

    for(i=0;i<(int)row;i++){

        NSString *s1=[_levelfill objectAtIndex:i]; //levelfill is datasource array

        NSLog(@"s1 value is  %@",s1);
        NSString *s2=[_fillvalue objectAtIndex:i];
        s1=[s1 stringByAppendingString:@"  "];
        s1=[s1 stringByAppendingString:s2];
        [tval addObject:s1];

    }
    [pasteboard writeObjects:tval];

我希望输出是从 tableview 而不是 tableview 的数据源复制的。还有一种方法可以复制为表格本身,可以直接在 Microsoft Word 中粘贴为表格。

【问题讨论】:

    标签: ios objective-c cocoa


    【解决方案1】:

    你应该这样做

    NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
    [pasteboard clearContents];
    
    NSMutableArray *tval = [[NSMutableArray alloc]init];
    
    //This is the equivalent of your for
    [_levelfill enumerateObjectsUsingBlock:^(NSString *object, NSUInteger idx, BOOL *stop) {
        NSLog(@"_levelfill value is %@", object);
        NSString *s2 = [_fillvalue objectAtIndex:idx];
        NSString *s1 = object;
        s1 = [s1 stringByAppendingString:@"  "];
        s1 = [s1 stringByAppendingString:s2];
        // Create the string representing the row and add it to the array
        [tval addObject:s1];
    }];
    
    // Set the string in the pasteboard
    [pasteboard declareTypes:[NSArray arrayWithObject:NSPasteboardTypeString] owner:nil];
    [pasteboard setString:[tval componentsJoinedByString:@"\n"] forType:NSPasteboardTypeString];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-10
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      相关资源
      最近更新 更多