从 uitableview 填充电子邮件正文的最佳方式

发布于 2024-12-15 02:29:22 字数 309 浏览 2 评论 0原文

好吧,让我试着提出一个更好的问题。

我的应用程序中有一个 tableViewController ,其中包含用户希望通过电子邮件向某人发送来自 tableview 的数据的一些信息。

我可以保留一个包含该信息的数组。

问题的困难部分是如何使用 Objective-C 语言使用表视图中的数据填充消息正文?

我是否必须制作一个包含所有 html 代码的巨大字符串?或者有更好/更简单的方法吗?即使它只是一个看起来很简单的表格,以便客户将其发送给某人。

我想任何解决方案/建议都可能是我知道应该如何处理这个问题的一个很好的途径。

Ok let me try to make it a better question.

I have a tableViewController in my app containing some info that the user would like to email someone with that data from the tableview.

I could retain an array with that information.

The tough part of the question is how do i populate the body of the message with the data from my tableview using objective-c language ?

Do I have to make a huge string containing all the html code ? Or is there a better/easier way around it ? Even if it is a simple looking table just so the client will send that out to someone.

I guess any solution/advice could be a great waypoint for me to know how should i work with this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乄_柒ぐ汐 2024-12-22 02:29:22

我真是搞定了!
如果你们想写一封包含 UITableViewController 中数据的电子邮件
这就是我的制作方法。只需记住导入您的数据头文件...

#import Recipe.h //in the implementation file
#import Ingredients.h //in the implementation file
#import <MessageUI/MFMailComposeViewController.h> // this line gotta be in the header file

-(IBAction)sendmail{
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
[composer setMailComposeDelegate:self];
NSString *recipeTitle = @"<h5>Recipe name: ";
recipeTitle = [recipeTitle stringByAppendingFormat:recipe.name];
recipeTitle = [recipeTitle stringByAppendingFormat:@"</h5>"];

NSString *ingredientAmount = @"";
NSString *ingredientAisle = @"";
NSString *ingredientTitle = @"";

NSString *tableFirstLine = @"<table width='300' border='1'><tr><td>Ingredient</td><td>Amount</td><td>Aisle</td></tr>";
NSString *increments = @"";
increments = [increments stringByAppendingFormat:recipeTitle];
increments = [increments stringByAppendingFormat:tableFirstLine];
int i;

for (i=0; i < [ingredients count]; i++) {
    Ingredient *ingredient = [ingredients objectAtIndex:i];
    ingredientTitle = ingredient.name;
    ingredientAmount = ingredient.amount;
    ingredientAisle = ingredient.aisle;

    increments = [increments stringByAppendingFormat:@"<tr><td>"];
    increments = [increments stringByAppendingFormat:ingredientTitle];
    increments = [increments stringByAppendingFormat:@"</td><td>"];
    increments = [increments stringByAppendingFormat:ingredientAmount];
    increments = [increments stringByAppendingFormat:@"</td><td>"];
    increments = [increments stringByAppendingFormat:ingredientAisle];
    increments = [increments stringByAppendingFormat:@"</td></tr>"];
    if (i == [ingredients count]) {
        //IF THIS IS THE LAST INGREDIENT, CLOSE THE TABLE
        increments = [increments stringByAppendingFormat:@"</table>"];
    }
}

NSLog(@"CODE:: %@", increments);

if ([MFMailComposeViewController canSendMail]) {
    [composer setToRecipients:[NSArray arrayWithObjects:@"[email protected]", nil]];
    [composer setSubject:@"subject here"];
    [composer setMessageBody:increments isHTML:YES];
    [composer setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [self presentModalViewController:composer animated:YES];
    [composer release];
}else {
    [composer release];
    }
}

这对我来说是一大步,如果您想在应用程序中创建有趣的(基本)工具,它实际上非常有用。
感谢这个面向 Objective-C 程序员的经典网站中的每个人。

这就是你得到的结果。简单但却是一个很好的开始方式。

在此处输入图像描述

I freaking nailed it !
If you guys ever want to compose an email having the data from your UITableViewController
here's how i made it. Just remember to import your data header file...

#import Recipe.h //in the implementation file
#import Ingredients.h //in the implementation file
#import <MessageUI/MFMailComposeViewController.h> // this line gotta be in the header file

-(IBAction)sendmail{
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
[composer setMailComposeDelegate:self];
NSString *recipeTitle = @"<h5>Recipe name: ";
recipeTitle = [recipeTitle stringByAppendingFormat:recipe.name];
recipeTitle = [recipeTitle stringByAppendingFormat:@"</h5>"];

NSString *ingredientAmount = @"";
NSString *ingredientAisle = @"";
NSString *ingredientTitle = @"";

NSString *tableFirstLine = @"<table width='300' border='1'><tr><td>Ingredient</td><td>Amount</td><td>Aisle</td></tr>";
NSString *increments = @"";
increments = [increments stringByAppendingFormat:recipeTitle];
increments = [increments stringByAppendingFormat:tableFirstLine];
int i;

for (i=0; i < [ingredients count]; i++) {
    Ingredient *ingredient = [ingredients objectAtIndex:i];
    ingredientTitle = ingredient.name;
    ingredientAmount = ingredient.amount;
    ingredientAisle = ingredient.aisle;

    increments = [increments stringByAppendingFormat:@"<tr><td>"];
    increments = [increments stringByAppendingFormat:ingredientTitle];
    increments = [increments stringByAppendingFormat:@"</td><td>"];
    increments = [increments stringByAppendingFormat:ingredientAmount];
    increments = [increments stringByAppendingFormat:@"</td><td>"];
    increments = [increments stringByAppendingFormat:ingredientAisle];
    increments = [increments stringByAppendingFormat:@"</td></tr>"];
    if (i == [ingredients count]) {
        //IF THIS IS THE LAST INGREDIENT, CLOSE THE TABLE
        increments = [increments stringByAppendingFormat:@"</table>"];
    }
}

NSLog(@"CODE:: %@", increments);

if ([MFMailComposeViewController canSendMail]) {
    [composer setToRecipients:[NSArray arrayWithObjects:@"[email protected]", nil]];
    [composer setSubject:@"subject here"];
    [composer setMessageBody:increments isHTML:YES];
    [composer setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [self presentModalViewController:composer animated:YES];
    [composer release];
}else {
    [composer release];
    }
}

This one was a huge step for me and it is actually very useful if you want to create interesting (basic) tools in your app.
Thanks to everyone in this quintessential website for objective-c programmers.

Andthis is the result you get. Simple but a good way to start.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文