Добавление нескольких строк в строку - PullRequest
5 голосов
/ 14 ноября 2011

Как добавить несколько строк в строку? Какой самый простой способ сделать это? Если я не хочу создавать новую строку кода каждый раз, когда добавляю что-то в строку, я бы хотел сделать что-то подобное:

    NSString *recipeTitle = [@"<h5>Recipe name: " stringByAppendingFormat:recipe.name, @"</h5>"];
    NSLog(@"%@", recipeTitle);

    // This shows: <h5>Recipe name: myrecipe
    // Where's the </h5> closing that header ? It will only show up with the next line of code

    recipeTitle = [recipeTitle stringByAppendingFormat:@"</h5>"];

    //my problem is that will result in more than 1k lines of programming

Нужно ли обязательно добавлять новую строку, добавляя каждый раз добавленную? Есть ли более быстрый / более производительный способ сделать это?

Я пытаюсь составить тело письма с моим табличным представлением, и это приведет к огромному набору строк программирования. Есть ли кто-нибудь, кто мог бы дать мне какой-либо совет или что-нибудь лучше, чем составить строку huuuge, просто чтобы я мог заполнить свое тело письма таблицей, содержащей мои данные таблицы?

Мы ценим любую помощь, чтобы сделать это более продуктивным. Спасибо ! Карлос Фарини.

// Поработав немного, я получил:

-(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='90%' border='1'><tr><td>Ingredient</td><td>Amount</td><td>Aisle</td></tr>";
NSString *increments = @"";
int i=0;

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

    increments = [increments stringByAppendingFormat:recipeTitle];
    increments = [tableFirstLine 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]-1)) {
        //IF THIS IS THE LAST INGREDIENT, CLOSE THE TABLE
        increments = [increments stringByAppendingFormat:@"</table>"];
    }
}

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

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

}

Но опять же, он показывает только одну строку в таблице. Что я тут не так делаю?

1 Ответ

5 голосов
/ 14 ноября 2011

Как насчет этого:

NSString *recipeTitle = [NSString stringWithFormat:@"<h5>Recipe name: %@ </h5>", recipe.name]; 
...