Question

I am using Storyboards, in that I have a customised table. I need to display value on a label which is inside a cell. I tried creating an IBOutlet for it but it doesn't seem to accept it, it gives me an error saying, "connection "xyz" cannot have a prototype object as its destination"

Was it helpful?

Solution

Found the solution, I dynamically created a Label and set value to it ` if (indexPath.row == 2) { static NSString *Identifier1 = @"Cell3";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier1];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier1];
        cell.textLabel.lineBreakMode=NSLineBreakByCharWrapping;
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15.0];

    }

    UILabel *txtDate = [[UILabel alloc] initWithFrame:CGRectMake(13.0f, 26.0f, 294.0f, 30.0f)];
    txtDate.text = stringFromDate;

    [txtDate setUserInteractionEnabled:NO];
    txtDate.font = [UIFont fontWithName:@"Helvetica" size:15.0];
    [cell.contentView addSubview:txtDate];

    return cell;


}`

OTHER TIPS

In a table view that has dynamic cells, you can not connect an IBOutlet in the table view controller to the elements inside the cell because the cell is a prototype object. This will be a different case if you set the table view to have static cells.

What's the difference between static cells and dynamic prototypes?

Because you are using dynamic cells, You need to subclass the UITableViewCell, create an outlet for the label in the cell subclass, and connect it to the UILabel in your Storyboard.

Another option would be just to use static table view cells if you know beforehand the rows will not change in terms of number and such.

iOS 10+ & Swift 3+, use this

let Identifier1 = "Cell3"

var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(Identifier1)
        if cell == nil {
            cell = UITableViewCell(style: UITableViewCellStyleDefault, reuseIdentifier: Identifier1)
            cell.textLabel.lineBreakMode = NSLineBreakByCharWrapping
            cell.textLabel.numberOfLines = 0
            cell.textLabel.font = UIFont(name: "Helvetica", size: 15.0)
        }
        var txtDate: UILabel = UILabel(frame: CGRectMake(13.0, 26.0, 294.0, 30.0))
        txtDate.text = stringFromDate
        txtDate.userInteractionEnabled = false
        txtDate.font = UIFont(name: "Helvetica", size: 15.0)
        cell.contentView.addSubview(txtDate)
        return cell
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top