简体   繁体   中英

Accessing Label value in a Custom Cell

I am new to Xcode so please be a with me. I have been able to populate my tableview using a Custom Cell, the custom cell has 3 labels on it.

I need to access the value of one of the labels (myDateLabel) when the row is selected.

For a 'standard' cell I know I could use something like:

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *theValue = cell.textLabel.text;

but I just can't figure out how to do this when using a custom cell, any help would be appreciated, I'm sure I'm missing something simple

You can set tag property of your labels in storyboard (if you're using it) or in code when creating labels.

label1.tag = 1001;
label2.tag = 1002;
label3.tag = 1003;

Then you can enumerate through cell's subViews and check the tag of subview

for (UIView *subview in customCell.subviews)
    switch (subview.tag)
    {
          case 1001:
           NSString *s = [(UILabel)subview text];
           break;
    }

Change you code from:

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *theValue = cell.textLabel.text;

To:

CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.myDateLabel.text = @"text";

There's a great beginner tutorial here: http://www.appcoda.com/customize-table-view-cells-for-uitableview/

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