i want to show 4 options(strings) randomly on uitableviewcell how do i achieve this???
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;
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];