Search code examples
uitableviewios6

How to get the cell.label.text values from a selected row of UITableview?


I am new in iOS application Development and i need help on UItableview. As i am populating the table-view with json data.I am also Using Customized Tableviewcell also. I am not getting the "huid" value of selected cell on "lbluid". The "lbluid" is showing value of "huid" according to scroll as i will scroll up it will show the "huid" value of upper most visible cell of the UITableview and if i scroll down it will show the "huid" value of last visible cell from down.

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableCell";
    SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)  {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];;
    }

    NSUInteger row = [indexPath row];

    cell.lbl4.text = [hudid objectAtIndex:row];
    lbluid.text=cell.lbl4.text;
    return cell;
}

Simpletablecell is the customized UITableViewCell.


Solution

  • You may have to implement the method:

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

    which is called each time a row is selected, and inside that method:

    Simpletablecell *cell = (Simpletablecell *)[tableView cellForRowAtIndexPath:indexpath];
    lbluid.text = cell.lbl4.text;
    

    The method:

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

    is called the the table view is loading it's cells to be displayed, which is why you lbluid.text is only changing when the table view is scrolled.