2014-05-20 67 views
-4
NSInteger i; 
for (i=0; i<10; i++) { 
    if (indexPath.row == i) { 
     UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yoman"]]; 
     imageView.frame = CGRectMake(0.0f, 0.0f, 40.0f, 27.0f); 
     [cell.contentView addSubview:imageView]; 
     [imageView release]; 
    } 
} 

這裏是我想我想要的是字符串+整數?在Objective-C

......:[UIImage imageNamed:@"yoman"+i]]; 

我怎樣才能做到這一點?

+0

'[UIImage的imageNamed:[的NSString stringWithFormat:@「yoman% d「,i]]' –

+0

似乎你並不是真的想用正確的方式學習iOS。正如我在主題(http://stackoverflow.com/questions/23731686/how-can-i-connect-uitableview-to-my-appdelegate-m)中所建議的那樣,我沒有像閱讀基本概念的書一樣閱讀,而是喜歡問任何東西。儘管我給出了一個答案但是低估了。再一次:減速:) –

+0

爲什麼我需要減速!當我實現我面臨一個問題,所以這個問題的學習!我知道很快就能在短時間內學會這件事!我告訴你,我只知道在5天內 –

回答

1

使用以下命令:

NSString *imageName = [NSString stringWithFormat:@"yoman%d", i]; 

然後:

UIImageView *imageView = [[UIImageView alloc] initWithImage:imageName]; 
+0

在這種情況下,它的工作原理,但我想要的就像我提到的! –

+0

你是什麼意思? – giorashc

+1

我想他想要做像C++運算符重載...不知道如果可能客觀C。 – Zhang

0

在OBJ-C,你只能追加串在一起,不是整數。

[UIImage imageNamed:[NSString stringWithFormat:@"yoman%i", (int)i]]; 

您最初試圖代碼(@"yoman"+i)將採取字符串指針(內存地址,它是一個整數),並補充:但是你可以使用標準「的sprintf」格式嵌入在一個字符串整數i它。這會給你一個內存地址到RAM中的未知位置,如果你使用它的任何東西,你的應用程序可能會崩潰。或者更糟的是,它可能不會崩潰......它可能只是做一些完全不同於你想要的東西。

+0

謝謝你的解釋! –

0

您可以使用+[NSString stringWithFormat:]構建的名稱,如

img = [UIImage imageNamed:[NSString stringWithFormat:@"yoman%d.png",i]]; 
+0

謝謝你這一個 –

0

您可以使用類方法的NSString

+ (instancetype)stringWithFormat:(NSString *)format 

使用這樣

[UIImage imageNamed:[NSString stringWithFormat:@"yoman%i", i]]; 
+0

酷!很好! –

1

這是這樣一個簡單的問題,但以防萬一你需要顯示fo R實施例 「1」 作爲 「01」( 「0」 之前的 「1」)對於那些小於10時,使用這一個:

NSString *imageName = [NSString stringWithFormat:@"yoman%02d", i]; 
相關問題