Search code examples
iphoneobjective-cuitableviewios5

Xcode 4.2 UITableview Custom cell


I have a problem with Custom cell on story board. I need to access labels from the method called

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

How can i define it in my code? When I used IBOutlet Then It may caused the error.

So how could I access the label like

cell.textlable.text ??

enter image description here

Thanks alot.


Solution

  • One common solution is to give each label a tag in the storyboard, and then find the label using viewWithTag: to find it in your code, like this:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = blah blah blah ...
        ...
        UILabel *myLabel = [cell viewWithTag:1];
        UILabel *anotherLabel = [cell viewWithTag:2];
        // etc.
    }