nilkash
nilkash

Reputation: 7536

Changing background color for selected row on first load of table view iphone

Hi I am developing small IOS application in which I am loading some data inside tableview. So what I want to do I want to make first row as default selected row with some selected background color. So I tried following things

  // inside viewDidLoad make first row as selected row 
    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
[_tableView selectRowAtIndexPath:indexPath animated:YES  scrollPosition:0];

 // inside cellForRowAtIndexPath set some selection color 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIView *bgColorView = [[UIView alloc] init];
    bgColorView.backgroundColor = [customColor colorWithHexString:@"91979755"];
    [cell setSelectedBackgroundView:bgColorView];
}

But it is not working. Any one know how to do this? Need some help. Thank you.

Upvotes: 0

Views: 1222

Answers (2)

Hiren
Hiren

Reputation: 686

Try this code, put this in cellForRow method:

UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor cyanColor];
[cell setSelectedBackgroundView:bgColorView];

And for specific row put this in if condition.

Upvotes: 0

JIthin
JIthin

Reputation: 1453

    self.indexPath = [NSIndexPath indexPathForRow:0 inSection:0];


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

     // inside cellForRowAtIndexPath set some selection color 
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {

        if(self.indexPath.row==indexPath.row){

     UIView *bgColorView = [[UIView alloc] init];
        bgColorView.backgroundColor = [customColor colorWithHexString:@"91979755"];
        [cell setSelectedBackgroundView:bgColorView];
       }


    }
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.indexPath=indexPath;
}

Upvotes: 0

Related Questions