Rosh
Rosh

Reputation: 169

Can't get text from didSelectRowAtIndexPath

I have added tableview on top of UIViewController and I have also implemented the UITableViewDataSource and UITableViewDelegate methods

For some reason, I am not getting the value out of didSelectRowAtIndexPath. I get the expected value of indexPath.row but the selectedCell.textLabel.text returns nil.

I am not able to figure out what may be the problem. I am using the dynamic table.

//Array of multiple objects is filled through the delegated arrays and the arrays are properly filled.
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return _arrayForMultipleObjects.count;
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    array = [[NSMutableArray alloc]init];

    // Configure the cell...
   _singleSelectionLabel= (UILabel *) [cell viewWithTag:2];
   _singleSelectionLabel.text=[self.arrayForMultipleObjects objectAtIndex:indexPath.row];


        return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *valu ;
    NSLog(@"%i count",indexPath.row);
    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *cellText = selectedCell.textLabel.text;
    NSLog(@"%@",cellText);
//Here cellText is null
    if([tableView cellForRowAtIndexPath:indexPath].accessoryType==UITableViewCellAccessoryNone)
    {   [tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryCheckmark;

        valu = cellText;
    }

    NSLog(@"%@",valu);
}

Upvotes: 0

Views: 288

Answers (1)

Abhinav
Abhinav

Reputation: 38162

If you are using storyboard then the default cell style of table cells is Custom which gives you a blank cell where you can add other types of controls to it.

If you dragged a label onto the cell and changed its text, then you are likely using a Custom style of UITableViewCell.

The property textLabel is only available on cells of types other than Custom, such as Basic, or Detailed.

Please double check if this is the case for you.

Edit: As ready suggested you can fetch the text like this:

self.arrayForMultipleObjects[indexPath.row]

Upvotes: 1

Related Questions