2009-06-17 44 views

回答

2

使用,而不是anormal陣列一個NSMutableArray,然後使用一個隨機函數來得到這樣一個隨機指數:

int rand = arc4random() % [yourMutableArray count]; 

然後得到的值,並做一個:

[yourMutableArray removeObjectAtIndex:rand]; 
0

取決於你的意思是什麼「秀4個選項隨機」。

我假設你想顯示在UITableView的一個單元一個串出四。

創建UITableDataSource,存儲在陣列4串和時的UITableView請求返回使用隨機函數的4串中的一個對選擇的行細胞。

看一看蘋果開發者文檔瞭解如何實施的UITableView /數據源所需的方法。

替換:

// i have 4 strings in the array listOfOptionsText 
cell.text = [listOfOptionsText objectAtIndex:indexPath.row]; 
return cell; 

有了:

int rand = arc4random() % [listOfOptionsText count]; 
cell.text = [listOfOptionsText objectAtIndex:rand]; 
return cell; 

回覆:複製

如果你要重複我假設你想顯示4串(最終) 如果你想以隨機順序顯示4個值,那麼你可以先將字符串洗牌,然後在ord中選擇它們呃,洗牌例如:

NSMutableArray * deck = 
[[NSMutableArray alloc] initWithObjects: @"One", @"Two", @"Three", @"Four", nil]; 

for (id string in deck) NSLog(@"%@", string); 

int pos = 0; 
int next = 0; 
int i; 
for (i = 0; i < 10; ++i) 
{ 
    next = arc4random() % [deck count]; 
    [deck exchangeObjectAtIndex:pos withObjectAtIndex:next]; 
    pos = next; 
} 

NSLog(@"after shuffle ..."); 
for (id string in deck) NSLog(@"%@", string); 

[deck release]; 

如果在初始化過程洗牌字符串,那麼你可以隨便挑,以便他們(asuming你不想重複這意味着你挑選4串出4 ...)。我不確定究竟是什麼目的。

現在,您可以設置單元格值時,再回到原來的代碼:

cell.text = [listOfOptionsText objectAtIndex:indexPath.row]; 
return cell; 
+0

是的,你是對的,但我怎麼shwo隨機字符串。 這裏是我的代碼 cell.text = [listOfOptionsText objectAtIndex:indexPath.row]; \t \t return cell; 我有4個字符串在數組listOfOptionsText。 你可以請發佈代碼 – 2009-06-17 12:24:54

+0

我得到重複值 – 2009-06-17 14:26:35