从数组中获取字符串,替换它们并将它们放在表中

时间:2014-08-06 18:01:58

标签: objective-c replace nsstring nsarray

我使用数组“allscores”来解析填充的表格行中的文本标签“scorelbl”。

cell.scorelbl.text = [allscores objectAtIndex:[indexPath row] - 1];

数组打印

1
2
3
2
etc

但是我想在每个字符串中包含“/ 5”,以便在单元格中打印:

1/5
2/5
3/5
2/5
etc

然后我用它来替换数组中的字符串:

for (int i=0; i<[allscores count]; i++) {


                NSString* string = [allscores objectAtIndex:i];

                NSString * scorestringformatted = [NSString stringWithFormat:@"%@/5", string];


                [allscores replaceObjectAtIndex:i withObject:scorestringformatted];
            }

我正在尝试但它打印出来:

1/5
2/5/5
3/5/5/5
2/5/5/5/5
etc

我应该修理什么?

根据建议的解决方案更新了结果:

当我为每一行打印时,/ 5不断增加。它从所需的一个开始,最后到

scores:
3/5/5/5/5/5/5,1/5/5/5/5/5/5,1/5/5/5/5/5/5,1/5/5/5/5/5/5,1/5/5/5/5/5/5,1/5/5/5/5/5/5,1/5/5/5/5/5/5,1/5/5/5/5/5/5,2/5/5/5/5/5/5,1/5/5/5/5/5/5,1/5/5/5/5/5/5,1


result: (
    "3/5/5/5/5/5/5/5",
    "1/5/5/5/5/5/5/5",
    "1/5/5/5/5/5/5/5",
    "1/5/5/5/5/5/5/5",
    "1/5/5/5/5/5/5/5",
    "1/5/5/5/5/5/5/5",
    "1/5/5/5/5/5/5/5",
    "1/5/5/5/5/5/5/5",
    "2/5/5/5/5/5/5/5",
    "1/5/5/5/5/5/5/5",
    "1/5/5/5/5/5/5/5",
    1
)


allscores: (
    "3/5/5/5/5/5/5/5",
    "1/5/5/5/5/5/5/5",
    "1/5/5/5/5/5/5/5",
    "1/5/5/5/5/5/5/5",
    "1/5/5/5/5/5/5/5",
    "1/5/5/5/5/5/5/5",
    "1/5/5/5/5/5/5/5",
    "1/5/5/5/5/5/5/5",
    "2/5/5/5/5/5/5/5",
    "1/5/5/5/5/5/5/5",
    "1/5/5/5/5/5/5/5",
    1
)

1 个答案:

答案 0 :(得分:1)

试试这样:

NSString *scores = [allscores componentsJoinedByString:@","];
scores = [scores stringByReplacingOccurrencesOfString:@"," withString:@"/5,"];

// Here is the result
NSArray *result = [scores componentsSeparatedByString:@","];

此处result将成为:

1/5
2/5
etc

使用result作为UITableView的数据源或将其复制到allscore,然后tableview的单元格将在单元格中显示上述值。