Take the 2-minute tour ×

I have what I think is a simple requirement. I have a table view controller and the cells are populated through Core Data information. That works. What I now want to do is extract the information from cell.textLabel.text in the cellForRowAtIndexPath to a string, which I will eventually use to create a PDF with.

To put this into perspective, I have a navigation bar button which creates a PDF:

- (IBAction)generatePDFbuttonPressed:(id)sender
    pageSize = CGSizeMake(612, 792);
    NSString *fileName = @"new.pdf";
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];

    [self generatePdfWithFilePath:pdfFileName];

That calls:

- (void) generatePdfWithFilePath: (NSString *)thefilePath
    UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);
    BOOL done = NO;
        //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.

This calls the drawText method:

- (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];

This draws out a PDF with the words Hello, this is a text. That's good. But not what I want.

Each cell is retrieving information from Core Data to populate the textLabel and detailTextLabel. The view controller title and section names are also achieved through a fetchedResultsController which does a fetchRequest into the Entity to retrieve that information. That works.

What I want to now do is take the cell.textLabel.text value, place it into a string and use that in the drawText method.

I have created a fetchRequest in the drawText method:

 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];

If I create a string with the types array, it's showing me the raw core data information in the PDF file. That proves it's working. However, that is not human readable.

In the cellForRow, a simple NSLog(@"%@", cell.textLabel.text) illustrates the correct information in the log.

What I want to do now is store that information into a string, pass it to the drawText method and USE that string to display information in the PDF file, via the NSString call in the drawText method.

This is driving me crazy and I cannot find a single solution for what I want to achieve; if someone can offer any assistance with this, I would be tremendously grateful!

In a nutshell, basically what I want to do is:

Retrieve WHATEVER is stored in the cell.textLabel.text and cell.detailTextLabel.text and have that saved to a string so that I can call that from my drawText method.

If this will not work with PDFs, can I email out the table information. I somehow need to SHARE out whatever is in this table view (including non visible cells) - whether it is creating a PDF, emailing or any other mechanism. I am at a real loss here!

Thank you!

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

The result of the fetch request is an array of managed objects, so you just have to iterate over that array and "draw" the desired information. For example

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

So you should get the data from the "model" (the fetched results controller in this case) and not from the "view" (the table view cells).

share|improve this answer
Thanks Martin - that is insanely helpful and having tested it out, it works like a charm with just a few minor hitches. 1) (stupid question), but how do I generate each set on a different line? So in the code above, name and other property works really well, but when there are two entries, how do I have each new set of name and other property generated on a separate line? 2) Should I omit the fetchRequest created in the drawText and just use the fetchedResultsController (which does the exact same fetchRequest)?.. if so, how would I call that in this drawRect method? - Thanks again Martin –  amitsbajaj Nov 27 '13 at 19:22
@amitsbajaj: 1) renderingRect determines where the text is drawn, so you could e.g. increment renderingRect.origin.y by some amount for a new line. 2) A fetched results controller is used as data source for a table view. In this case, a normal fetch request is the right thing. –  Martin R Nov 27 '13 at 19:27
Thanks @Martin - again super helpful. I will look into the renderingRect issue - thanks very much Martin. Once again. Life saver! –  amitsbajaj Nov 27 '13 at 20:02
add comment

Your Answer


