我正在使用数据源和委托实现基于视图的NSTableView。 我有一个代表电子邮件对象的自定义NSTableCellView。这个NSTableCellView有一个' body'文本域;使用数据填充文本字段的计算成本很高,因此我不想在显示NSTableView之前计算所有正文字符串。所以我尝试了这个:
NSTableViewDataSource:
-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return self.myMessages.count;
}
NSTableViewDelegate:
- (NSView *)tableView:(NSTableView *)tableView
viewForTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row {
EmailTableCellView *view = [tableView makeViewWithIdentifier:@"EmailTableCellView" owner:NULL];
view.from.stringValue = [self.myMessages[row] valueForKey:@"from"];
view.subject.stringValue = [self.myMessages[row] valueForKey:@"subject"];
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setDateFormat:@"MM-dd-YYYY"];
if ([self.myMessages[row] valueForKey:@"emailDate"] != NULL) {
NSDate *date = [self.myMessages[row] valueForKey:@"emailDate"];
view.emailDate.stringValue = [formatter stringFromDate:date];
}
if ([self.myMessages[row] valueForKey:@"body"] != NULL) {
view.body.stringValue = [self.myMessages[row] valueForKey:@"body"];
} else {
dispatch_queue_t backgroundQueue =
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
dispatch_async(backgroundQueue,^{
NSData *data = [NSData dataWithContentsOfFile:self.window.title options:NSDataReadingMappedAlways error:NULL];
NSRange messageRange = NSMakeRange([[self.myMessages[row] valueForKey:@"offset"] integerValue],[[self.myMessages[row] valueForKey:@"length"] integerValue]);
NSData *subData = [data subdataWithRange:messageRange];
NSString *myMessageString = [[NSString alloc] initWithData:subData encoding:NSUTF8StringEncoding];
CkoEmail *myEmail = [[CkoEmail alloc] init];
BOOL success = [myEmail SetFromMimeText:myMessageString];
if (success) {
if ([myEmail HasHtmlBody]) {
NSString *body = [myEmail GetHtmlBody];
body = [[[NSAttributedString alloc] initWithData:[body dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:nil error:nil] string];
dispatch_async(dispatch_get_main_queue(),^{
view.body.stringValue = body;
});
} else if ([myEmail HasPlainTextBody]) {
NSString *body = [myEmail GetPlainTextBody];
dispatch_async(dispatch_get_main_queue(),^{
view.body.stringValue = body;
});
}
}
});
}
return view;
}
当我打开包含桌子的窗口时,我得到旋转轮很长一段时间,在它停止旋转后我可以滚动几行然后再次挂起。
非常感谢任何帮助。
由于