2009-06-17 47 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