Eddie
Eddie

Reputation: 999

How to select row in uitableview programmatically based on NSString

I have a uitableview that hosts a number of cells, in another part of the app the user will make an NSString selection that will match on of the cells in the uitableview, how can I select hat cell programmatically?

I tried using the method

 selectRowAtIndexPath:animated

but how can I get the indexpath? is there a better way?

Upvotes: 0

Views: 177

Answers (3)

wesley
wesley

Reputation: 859

The way you needs to get the indexpath from source array is

int selectionIndex;
if([sourceArray containsObject:userSelectionString])
{
  selectionIndex = [sourceArray indexOfObject:userSelectionString];
}

You have to check whether selection object is there or not before get the indexpath from sourceArray.then you have to use this indexpath for selectRowAtIndexpath:animated method

Upvotes: 0

Shaik Riyaz
Shaik Riyaz

Reputation: 11462

try this .... first try to get the cell number based on string

NSString * pUserSelectedString = @"abcd";
int index;
if([array containsObject:pUserSelectedString])
{
      index = [array indexOfObject:pUserSelectedString];
} 

Upvotes: 0

manujmv
manujmv

Reputation: 6445

I think you are loading data from an array. Then just search inside that NSArray and take index of the matched object. Now you can create NSIndexPath programatically:

[NSIndexPath indexPathForRow:index inSection:section];

Just use this NSIndexPath to select the row

Upvotes: 2

Related Questions