5

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

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:@"[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]; 
} 

}

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

ответ

5

Как о чем-то вроде этого:

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

можно поместить несколько% @, а затем идти, как ..., recipe.name, recipe.preptime, recipe.included? Я бы добавил три строки в одну строку кода. Это было бы неплохо ... – Farini

+1

Да, вы можете. Вы можете проверить документацию и на другие способы генерации строк. – Odrakir

+0

Это прекрасно. Благодарим вас за просвещение – Farini

Смежные вопросы