Search code examples
iosuitableviewuiaccessoryview

Access text from UILabel that is set as AccessoryView to a UITableView


A UITableView uses a UILabel as its Accessory View.

By using the following code, I am able to retrieve the UILabel but i cant figure how to access the text alone:

NSLog(@"text label: %@",[[self.frequencyTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]] accessoryView]);

LOG:

2014-04-15 12:50:16.433 SmartWatch[2771:70b] text label: <UILabel: 0xfb81870; frame = (205 4; 60 20); text = '12:50 PM'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0xfb619d0>>

i want to retrieve the text alone('12:50 PM').Thanks!


Solution

  • NSLog(@"text label: %@",((UILabel*)[[self.frequencyTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]] accessoryView]).text);
    

    However, this is not really secure, i believe it would be better to directly access your model, when you set your accessory view you should do something like :

    UILabel *label = [[UILabel alloc] init];
    label.text = [someArray objectAtIndex:indexPath.row];
    cell.accessoryView = label;
    

    And if you want the text just get it directly from your array

    NSLog(@"text label: %@",[someArray objectAtIndex:0]);