IOS代码更改表格html逻辑

时间:2017-09-20 08:35:17

标签: html objective-c

我正在开发使用objective-C的邮件,格式send是html

这是我的代码:

NSString *content = @"<table style=\"width:100%\">";
int i = 0;
    for (RecipeIngradientVo *object in listIngredients) {
        NSString *ingreString = [NSString stringWithFormat:@"%@", object.recipeIngradientVoDescription];

        if(i == 0){
            content = [NSString stringWithFormat:@"%@<tr style=\"height:10px;\"> <td valign=\"top\" style=\"padding-left:20px\"> <li>%@</li> </td>", content, ingreString];

            i++;
            NSLog(@"i : %d", i);
        }else{

            content = [NSString stringWithFormat:@"%@<td valign=\"top\" style=\"padding-left:20px\"> <li>%@</li> </td> </tr>", content, ingreString];
            i = 0;
            NSLog(@"i : %d", i);
        }
    }
content = [content stringByAppendingString:@"</table>"];

此代码将显示为

1 2

3 4

5 6

7

但我必须改为

1 5

2 6

3 7

4

我陷入了这种逻辑,plz帮助

2 个答案:

答案 0 :(得分:0)

您的逻辑可以被清理得更多,查看modulo进行翻转而不是if-else语句。但一般来说,你会想要手动拉出阵列:

NSString *content = @"<table style=\"width:100%\">";
int i = 0;
int index = 0;
    while (index < listIngredients.count) {

        if(i == 0){

int indexToUse = index;
RecipeIngradientVo *object = [listIngredients objectAtIndex:indexToUse];
        NSString *ingreString = [NSString stringWithFormat:@"%@", object.recipeIngradientVoDescription];

            content = [NSString stringWithFormat:@"%@<tr style=\"height:10px;\"> <td valign=\"top\" style=\"padding-left:20px\"> <li>%@</li> </td>", content, ingreString];

            i++;
            NSLog(@"i : %d", i);
        }else{

int indexToUse = index+floor(listIngredients.count/2);
RecipeIngradientVo *object = [listIngredients objectAtIndex:indexToUse];
        NSString *ingreString = [NSString stringWithFormat:@"%@", object.recipeIngradientVoDescription];

            content = [NSString stringWithFormat:@"%@<td valign=\"top\" style=\"padding-left:20px\"> <li>%@</li> </td> </tr>", content, ingreString];
            i = 0;
            NSLog(@"i : %d", i);
        }
index++;
    }
content = [content stringByAppendingString:@"</table>"];

答案 1 :(得分:0)

我找到了基于@Albert Renshaw帮助的答案:)

这是我的代码:

while (index < listIngredients.count)  {
        NSLog(@"index: %d", index);
        if(i == 0){
            indexToUse = index;
            RecipeIngradientVo *object = [listIngredients objectAtIndex:indexToUse];
            NSString *ingreString = [NSString stringWithFormat:@"%@", object.recipeIngradientVoDescription];
            content = [NSString stringWithFormat:@"%@<tr style=\"height:10px;\"> <td valign=\"top\" style=\"padding-left:20px\"> <li>%@</li> </td>", content, ingreString];

            i++;
            NSLog(@"indexToUse : %d", indexToUse);
        }else{
            indexToUse = indexToUse + round((listIngredients.count + 0.5)/2);
            if(indexToUse >= listIngredients.count){
                NSLog(@"indexToUse it max : %d", indexToUse);
                content = [NSString stringWithFormat:@"%@ </tr>", content];
                break;
            }
            RecipeIngradientVo *object = [listIngredients objectAtIndex:indexToUse];
            NSString *ingreString = [NSString stringWithFormat:@"%@", object.recipeIngradientVoDescription];
            content = [NSString stringWithFormat:@"%@<td valign=\"top\" style=\"padding-left:20px\"> <li>%@</li> </td> </tr>", content, ingreString];
            i = 0;
            NSLog(@"indexToUse : %d", indexToUse);
            index++;
        }
    }