Rashad
Rashad

Reputation: 11197

Can not select UITableViewCell programmatically

I am trying to select a row when the table view is loaded. I've found other thread regarding this issue but they didn't solve my problem so I am asking again.

My tableview contains 5 row. And I am doing this:

if ([[dataSource objectAtIndex:indexPath.row] isEqualToString:self.status]) {
    [self tableView:[self tableView] didSelectRowAtIndexPath:indexPath];
}

I've also tried cell.selected = YES; and cell.highlighted = YES;.

I am using the following code for custion selection view:

UIView *customColorView = [[UIView alloc] init];

customColorView.backgroundColor = [UIColor colorWithRed:180/255.0
                                                      green:138/255.0
                                                       blue:171/255.0
                                                      alpha:0.5];
cell.selectedBackgroundView =  customColorView;

Please help me selecting a row programmatically. The row is selected when I tap on the row, but can not select programmatically.

Upvotes: 1

Views: 1869

Answers (3)

Bhanu
Bhanu

Reputation: 1249

You can get this by calling UITableView delegate method which is

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {}

inside this method you need to check every selected time that same cell are you not tapped.

if ([[myArray objectAtIndex:indexPath.row] isEqualToString:self.same]) 
{
    [myTable selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
}

Upvotes: 3

iPatel
iPatel

Reputation: 47049

Try with following code

[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];

Here you need to set indexPath for row that you want to be selected.

Upvotes: 3

Simon
Simon

Reputation: 1394

Try calling selectRowAtIndexPath on the table view object:

if ([[dataSource objectAtIndex:indexPath.row] isEqualToString:self.status]) 
{
    [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
}

Upvotes: 1

Related Questions