Reputation: 1304
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";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
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;
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();
}
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!
Upvotes: 0
Views: 311
Reputation: 539705
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
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
}
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).
Upvotes: 1