views:

140

answers:

2

i want to show 4 options(strings) randomly on uitableviewcell how do i achieve this???

A: 

Depends what you mean by 'show 4 options randomly'.

I assume you want to show one cell in UITableView with one string out of four.

Create UITableDataSource, store 4 strings in an array and when UITableView requests cell for selected row return one of the 4 strings using random function.

Have a look at apple developer documentation about how to implement required methods for UITableView/DataSource.

Replace:

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

With:

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

Re: duplicates

If you're getting duplicates I assume that you want to show 4 strings (eventually) If you want to show 4 values but in random order then you can shuffle the strings first and then just pick them in order, shuffle example:

 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];

If you shuffle strings during initialization then you can just pick them in order (asuming you don't want duplicates meaning you are picking 4 strings out of 4 ...). I'm not sure what exactly is the purpose.

Now you can go back to original code when setting cell value:

cell.text = [listOfOptionsText objectAtIndex:indexPath.row];
return cell;
stefanB
yes you are right but how do i shwo random strings.here is my codecell.text = [listOfOptionsText objectAtIndex:indexPath.row]; return cell;i have 4 strings in the array listOfOptionsText.could you please post the code
Rahul Vyas
i am getting duplicate Values
Rahul Vyas
still getting duplicates please provide proper code where this line should i write cell.text = [listOfOptionsText objectAtIndex:indexPath.row]; return cell; for (id string in deck) NSLog(@"%@", string);should i write these 2 lines between this?[deck release];
Rahul Vyas
+1  A: 

Use an NSMutableArray instead of anormal array and then use a random function to get a random index like:

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

then get the value and do a:

[yourMutableArray removeObjectAtIndex:rand];
Dimitris
if you don't care about duplicates.
Roger Nolan
using this does not shows strings randomly
Rahul Vyas
You don't get duplicates from the code above. It's a NSMutableArray and every time you get a value:myString = [yourMutableArray objectAtIndex:rand];you then remove it from the Array. So you can\t get it again. The line of code above is the only one missing from my initial response to make this work (get the value).
Dimitris