【问题标题】:Store cell.textLabel.text in NSString将 cell.textLabel.text 存储在 NSString 中
【发布时间】: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


    【解决方案1】:

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

    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
    }
    

    因此,您应该从“模型”(在本例中为获取的结果控制器)获取数据 而不是来自“视图”(表格视图单元格)。

    【讨论】:

    • 感谢 Martin - 这非常有用,并且经过测试,它就像一个魅力,只有一些小故障。 1)(愚蠢的问题),但是如何在不同的行上生成每个集合?所以在上面的代码中,name 和其他属性工作得很好,但是当有两个条目时,我如何在单独的行上生成每组新的 name 和其他属性? 2) 我是否应该省略在 drawText 中创建的 fetchRequest 而只使用 fetchedResultsController (它执行完全相同的 fetchRequest)?.. 如果是这样,我将如何在这个 drawRect 方法中调用它? - 再次感谢马丁
    • @amitsbajaj: 1) renderingRect 确定文本的绘制位置,因此您可以例如将renderingRect.origin.y 增加一些新行。 2) 获取的结果控制器用作表视图的数据源。在这种情况下,正常的 fetch 请求是正确的。
    • 感谢@Martin - 再次非常有帮助。我将研究 renderingRect 问题 - 非常感谢 Martin。再来一次。救生员!
    最近更新 更多