MaSza
MaSza

Reputation: 465

How to get first cell of TableView?

I'm new in Objective-C and iOS develop and I have a strange problem, only in one specific situation. This is part of my switch code:

if(nowSolo>-1){
    [soloarray replaceObjectAtIndex:nowSolo withObject:[NSNumber numberWithBool:NO]];
    NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:nowSolo inSection:0];
    CustomCell *cell2=[_tableView cellForRowAtIndexPath:nowSolo];
    cell2.solo.backgroundColor=[UIColor colorWithRed:0.29 green:0.67 blue:0.62 alpha:1.0];
}
nowSolo=row;
[soloarray replaceObjectAtIndex:row withObject:[NSNumber numberWithBool:YES]];
cell.solo.backgroundColor=[UIColor colorWithRed:0.13 green:0.41 blue:0.37 alpha:1.0];

Everything goes fine but when variable nowSolo is equal 0 then cell2 = null. I don't understand why, because for other values like 1,2,3 etc it work correctly.

Upvotes: 1

Views: 2656

Answers (1)

vaibhav
vaibhav

Reputation: 4096

You can get the cell inside both methods cellForRowAtIndexPath & didSelectRowAtIndexPath like when you configuring it or when you touch.

Get specific cell when you configuring inside cellForRowAtIndexPath delegate method like below code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row == 0) {
        // configure cell here 
    }
    return cell;
}

Get specific cell when you touch using didSelectRowAtIndexPath method see below code:

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

     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
     if (indexPath.row == 0) {
          // do specific action on first cell 
     }
}

Upvotes: 2

Related Questions