0

我又有点卡住了。. .

我有一个带有一些自定义 NSTableCellView 的 NSTableView,其中一个包含几个 NSTextField。

现在,我想修改某些字段中显示的值,因此创建覆盖了创建单元格函数,如下所示:

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {

    // Get a new ViewCell
    NSTableCellView *cellView = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];

然后,我使用以下代码来定位特定列:

if ([tableColumn.identifier isEqualToString:@"Tweets"]){
         NSFetchRequest *request = [[NSFetchRequest alloc] init];
        //Set predicate and filter for New tweets page
        if ([self.currentTwitterView isEqualToString:@"new"]) {
        NSPredicate *testForTrue = [NSPredicate predicateWithFormat:@"(approved == NO) AND (tweetDeleted == NO)  AND (scheduledTweet == NO)"];
        NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"postDate" ascending:NO];
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, nil];
        [request setPredicate:testForTrue];
        [request setSortDescriptors:sortDescriptors];

        //Set filter and predicate for the Approved tweets page
    } else if ([self.currentTwitterView isEqualToString:@"approved"]){
        NSPredicate *testForTrue = [NSPredicate predicateWithFormat:@"(approved == YES) AND (tweetDeleted == NO)  AND (scheduledTweet == NO)"];
        NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"approvedDate" ascending:NO];
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, nil];
        [request setPredicate:testForTrue];
        [request setSortDescriptors:sortDescriptors];

        //Set filter and preicate for the Deleted tweets page
    } else if ([self.currentTwitterView isEqualToString:@"deleted"]){
        NSPredicate *testForTrue = [NSPredicate predicateWithFormat:@"tweetDeleted == YES"];
        NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"deletedDate" ascending:NO];
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, nil];
        [request setPredicate:testForTrue];
        [request setSortDescriptors:sortDescriptors];

        //Set filter and preicate for the Deleted tweets page
    } else if ([self.currentTwitterView isEqualToString:@"scheduled"]){
        NSPredicate *testForTrue = [NSPredicate predicateWithFormat:@"scheduledTweet == YES"];
        NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"scheduledDate" ascending:NO];
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, nil];
        [request setPredicate:testForTrue];
        [request setSortDescriptors:sortDescriptors];
    }
    //Setup the Request
    [request setEntity:[NSEntityDescription entityForName:@"Tweet" inManagedObjectContext:_managedObjectContext]];

    //Assign the predicate to the fetch request
    NSError *error = nil;

    //Create an array from the returned objects
    NSArray *fetchedObjects = [_managedObjectContext executeFetchRequest:request error:&error];

    Tweet *selectedTweet = [fetchedObjects objectAtIndex:row];

    [cellView.textField setStringValue:[NSString stringWithFormat:@"Access granted for %@", selectedTweet.scheduledDate]];

    return cellView;

但是,我一生都无法弄清楚如何定位单元格中的每个特定 TextField,如果我创建一个 IBOutlet,那么我会收到一个关于存在多个的错误(这是有道理的),但如果我不这样做,那么就没有办法访问它,我可以看到。

我查看了苹果类文档,也没有真正详细说明。

非常感谢帮助!

干杯

加雷斯

4

1 回答 1

0

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/TableView/PopulatingView-TablesProgrammatically/PopulatingView-TablesProgrammatically.html#//apple_ref/doc/uid/10000026i-CH14-SW5

里面有你需要的所有信息。

具体标记的部分:

Acquiring a Cell from Interface Builder
于 2013-07-31T10:48:13.607 回答