【发布时间】:2025-12-18 12:25:03
【问题描述】:
我有一个我认为很简单的要求。我有一个表格视图控制器,单元格是通过核心数据信息填充的。这样可行。我现在要做的是从 cellForRowAtIndexPath 中的 cell.textLabel.text 中提取信息到一个字符串,我最终将使用它来创建一个 PDF。
从这个角度来看,我有一个创建 PDF 的导航栏按钮:
- (IBAction)generatePDFbuttonPressed:(id)sender
{
pageSize = CGSizeMake(612, 792);
NSString *fileName = @"new.pdf";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
[self generatePdfWithFilePath:pdfFileName];
}
那叫:
- (void) generatePdfWithFilePath: (NSString *)thefilePath
{
UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);
BOOL done = NO;
do
{
//Start a new page.
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);
//Draw text fo our header.
[self drawHeader];
//Draw some text for the page.
[self drawText];
//Draw an image
[self drawImage];
done = YES;
}
while (!done);
// Close the PDF context and write the contents out.
UIGraphicsEndPDFContext();
}
这会调用drawText方法:
- (void)drawText
{
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(currentContext, 0.0, 0.0, 0.0, 1.0);
NSString *textToDraw = @"Hello, this is a test";
UIFont *font = [UIFont systemFontOfSize:14.0];
CGSize textSize = CGSizeMake(pageSize.width - 5*kBorderInset-5*kMarginInset, pageSize.height - 5*kBorderInset - 5*kMarginInset);
CGRect renderingRect = CGRectMake(kBorderInset + kMarginInset, kBorderInset + kMarginInset + 50.0, pageSize.width - 2*kBorderInset - 2*kMarginInset, textSize.height);
[textToDraw drawWithRect:renderingRect options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];
}
这会画出一个 PDF,上面写着“你好,这是一个文本”。那挺好的。但不是我想要的。
每个单元格都从 Core Data 中检索信息以填充 textLabel 和 detailTextLabel。视图控制器的标题和部分名称也是通过 fetchedResultsController 实现的,该控制器对实体执行 fetchRequest 以检索该信息。这样可行。
我现在要做的是获取 cell.textLabel.text 值,将其放入字符串中并在 drawText 方法中使用它。
我在 drawText 方法中创建了一个 fetchRequest:
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *pdfFetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Transaction" inManagedObjectContext:managedObjectContext];
pdfFetchRequest.entity = entity;
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"dates.dateOfEvent" ascending:NO];
pdfFetchRequest.sortDescriptors = [NSArray arrayWithObject:sort];
pdfFetchRequest.fetchBatchSize = 20;
pdfFetchRequest.predicate = [NSPredicate predicateWithFormat:@"whoBy = %@", self.person];
NSError *error = nil;
NSArray *types = [managedObjectContext executeFetchRequest:pdfFetchRequest error:&error];
NSString *textToDraw = [NSString stringWithFormat:@"Information = %@", types];
如果我使用 types 数组创建一个字符串,它会向我显示 PDF 文件中的原始核心数据信息。这证明它正在工作。但是,这不是人类可读的。
在 cellForRow 中,一个简单的 NSLog(@"%@", cell.textLabel.text) 说明了日志中的正确信息。
我现在要做的是将该信息存储到一个字符串中,将其传递给 drawText 方法,并通过 drawText 方法中的 NSString 调用使用该字符串在 PDF 文件中显示信息。
这让我发疯了,我找不到我想要实现的单一解决方案;如果有人可以提供任何帮助,我将不胜感激!
简而言之,基本上我想做的是:
检索存储在 cell.textLabel.text 和 cell.detailTextLabel.text 中的 WHATEVER 并将其保存到字符串中,以便我可以从我的 drawText 方法中调用它。
如果这不适用于 PDF,我可以通过电子邮件发送表格信息吗?我需要以某种方式分享此表格视图中的任何内容(包括不可见的单元格) - 无论是创建 PDF、电子邮件还是任何其他机制。我在这里真是不知所措!
谢谢!
【问题讨论】:
标签: ios iphone objective-c core-data uitableview