将cell.textLabel.text存储在NSString中



我有一个我认为很简单的需求。我有一个表视图控制器,单元格是通过核心数据信息填充的。这很管用。我现在想做的是将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,上面写着你好,这是一篇文本。这很好。但不是我想要的。

每个单元格都从核心数据中检索信息,以填充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文件中显示信息。

这让我抓狂,我找不到一个单一的解决方案来实现我想要实现的目标;如果有人能在这方面提供任何帮助,我将不胜感激!

简而言之,基本上我想做的是:

Retrieve WHATEVER存储在cell.textLabel.text和cell.detailTextLabel.text中,并将其保存到一个字符串中,以便我可以从drawText方法调用它。

如果这对PDF不起作用,我可以通过电子邮件发送表格信息吗。我需要以某种方式分享这个表视图中的任何内容(包括不可见的单元格)——无论是创建PDF、发送电子邮件还是任何其他机制。我在这里真的不知所措!

谢谢!

获取请求的结果是一个托管对象数组,因此您只需要迭代该数组并"绘制"所需的信息。例如

NSArray *types = [managedObjectContext executeFetchRequest:pdfFetchRequest error:&error];
for (Transaction *trans in types) {
    NSString *name = trans.name; // Assuming that there is a "name" property
    NSString *otherProperty = trans.otherProperty; // just an example
    NSString *textToDraw = [NSString stringWithFormat:@"name = %@, other property = %@", name, otherProperty];
    // now draw it to the PDF context
}

因此,您应该从"模型"(在这种情况下是提取的结果控制器)中获取数据而不是从"视图"(表视图单元格)。

最新更新