I have a tableview which contains some text and I have a label inside tableviewcell as subview, in the label there is also some values. My question is, when I tap the cell I want to get the label value, I know how to get a cell value when tapping the cell but I need to get the label value, my code for getting the tablecell value is
NSString *localStringValue;
localStringValue = [tableView cellForRowAtIndexPath:indexPath].textLabel.text;
and I display it in a textview textview.text = self.localStringValue;
My label name is chapterandverse, I need to implement this label instead of textlabel.text.
I add localStringValue = [tableView cellForRowAtIndexPath:indexPath].chapterandverse.text;
but I got error for this code. Please help me to do this.
Thanks in advance.
There’s 2 main ways to do this:
You can give the
chapterandverselabel atagvalue. In Interface Builder, there is a Tag property on the same page where you set the other properties like Text. If you’re doing it programmatically,chapterandverse.tag = 1;. Then you need to refer to the label by the tag value, like:You can do it by having an
@propertyreferring to the label in your custom table view cell class. You seem to have already tried to do this, but note that if you call[cell addSubview:chapterandverse]that does not mean you can later saycell.chapterandverse. To make it work properly, you need to make a subclass ofUITableViewCelland add the following declaration to it:And then synthesize it in the cell’s
.mfile:Then you can connect the label you placed in Interface Builder to the
chapterandverseoutlet on the view and refer to it bycell.chapterandverselike you are doing. If you create it programmatically, though, you will need to initialise theUILabelbefore using it: