2013-11-27 34 views
0

我有我认为是一个简单的要求。我有一个表视图控制器和单元格通过核心数据信息填充。这样可行。我现在想要做的是从cellForRowAtIndexPath中的cell.textLabel.text中提取信息为一个字符串,我将最终用它来创建一个PDF。在NSString中存储cell.textLabel.text

要地理解这一点,我有一个导航栏按钮,创建一个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]; 
} 

这会用文字Hello绘出一个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]; 

如果我创建的同类型数组一个字符串,它显示我在PDF文件中的原始核心数据信息。这证明它正在工作。但是,这不是人类可读的。

在cellForRow中,一个简单的NSLog(@“%@”,cell.textLabel.text)显示了日志中的正确信息。

我现在要做的就是将信息存储到字符串中,将其传递给drawText方法,并使用该字符串在drawText方法中通过NSString调用在PDF文件中显示信息。

这让我疯狂,我找不到一个解决方案来实现我想要的;如果有人可以提供任何援助,我会非常感激!

概括地说,基本上是我想要做的是:

检索无论是存储在cell.textLabel.text和cell.detailTextLabel.text并有保存到一个字符串,这样我可以调用从我的drawText方法。

如果这不适用于PDF,我可以通过电子邮件发送表格信息。我不知何故需要分享这个表格视图中的任何内容(包括不可见的单元格) - 无论是创建PDF,发送电子邮件还是其他任何机制。我在这里真正的损失!

谢谢!

回答

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 
} 

所以,你应该从“模型”(获取的成果控制器在这种情况下) ,而不是从“视图”(表视图细胞)获得的数据。

+0

感谢马丁 - 这是疯狂的帮助和测试它,它的作品就像一个魅力只有一些小小的麻烦。 1)(愚蠢的问题),但我如何生成不同线路上的每一组?所以在上面的代码中,名称和其他属性工作得很好,但是当有两个条目时,如何在单独的行上生成每个新的名称集和其他属性? 2)我应该省略drawText中创建的fetchRequest,并使用fetchedResultsController(它完全相同的fetchRequest)?如果是这样,我怎么会在这个drawRect方法中调用? - 再次感谢Martin – amitsbajaj

+0

@amitsbajaj:1)'renderingRect'确定文本在哪里绘制,所以你可以例如为一个新行增加'renderingRect.origin.y'一些数量。 2)获取的结果控制器用作表视图的数据源。在这种情况下,正常的获取请求是正确的。 –

+0

感谢@马丁 - 再次超级有用。我将研究renderingRect问题 - 非常感谢Martin。再来一次。拯救生命! – amitsbajaj

相关问题