简体   繁体   中英

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!

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]);

尝试这个

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM