我有一个switch语句,会适当地向11个UILabel添加文本。然而,现在它变得有点冗长了,而且有太多相同的代码副本。我该如何进一步改进它呢?也许我可以创建一个标签数组,然后通过for循环来访问它们?
这里是switch语句的代码:
switch ([number_sorted count]) {
case 1:
label_1.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[0] integerValue]];
break;
case 2:
label_1.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[0] integerValue]];
label_2.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[1] integerValue]];
break;
case 3:
label_1.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[0] integerValue]];
label_2.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[1] integerValue]];
label_3.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[2] integerValue]];
break;
case 4:
label_1.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[0] integerValue]];
label_2.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[1] integerValue]];
label_3.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[2] integerValue]];
label_4.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[3] integerValue]];
break;
case 5:
label_1.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[0] integerValue]];
label_2.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[1] integerValue]];
label_3.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[2] integerValue]];
label_4.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[3] integerValue]];
label_5.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[4] integerValue]];
break;
case 6:
label_1.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[0] integerValue]];
label_2.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[1] integerValue]];
label_3.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[2] integerValue]];
label_4.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[3] integerValue]];
label_5.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[4] integerValue]];
label_6.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[5] integerValue]];
break;
case 7:
label_1.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[0] integerValue]];
label_2.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[1] integerValue]];
label_3.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[2] integerValue]];
label_4.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[3] integerValue]];
label_5.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[4] integerValue]];
label_6.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[5] integerValue]];
label_7.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[6] integerValue]];
break;
case 8:
label_1.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[0] integerValue]];
label_2.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[1] integerValue]];
label_3.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[2] integerValue]];
label_4.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[3] integerValue]];
label_5.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[4] integerValue]];
label_6.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[5] integerValue]];
label_7.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[6] integerValue]];
label_8.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[7] integerValue]];
break;
case 9:
label_1.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[0] integerValue]];
label_2.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[1] integerValue]];
label_3.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[2] integerValue]];
label_4.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[3] integerValue]];
label_5.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[4] integerValue]];
label_6.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[5] integerValue]];
label_7.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[6] integerValue]];
label_8.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[7] integerValue]];
label_9.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[8] integerValue]];
break;
case 10:
label_1.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[0] integerValue]];
label_2.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[1] integerValue]];
label_3.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[2] integerValue]];
label_4.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[3] integerValue]];
label_5.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[4] integerValue]];
label_6.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[5] integerValue]];
label_7.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[6] integerValue]];
label_8.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[7] integerValue]];
label_9.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[8] integerValue]];
label_10.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[9] integerValue]];
break;
case 11:
label_1.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[0] integerValue]];
label_2.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[1] integerValue]];
label_3.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[2] integerValue]];
label_4.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[3] integerValue]];
label_5.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[4] integerValue]];
label_6.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[5] integerValue]];
label_7.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[6] integerValue]];
label_8.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[7] integerValue]];
label_9.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[8] integerValue]];
label_10.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[9] integerValue]];
label_11.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[10] integerValue]];
break;
default:
break;
}
感谢您抽出时间,丹。