James Patrick
James Patrick

Reputation: 263

uilabel from custom tableview cell is not displayed in tableviewcontroller

I have created a table view controller and custom cell .I have created a new tableviewCell file which has outlets for the labels in the custom cell .

I have imported the tableviewcell class in the tableviewController and tried to assign value from an array to the cell by the following code :

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier ];
}

cell.name.text= [data objectAtIndex:indexPath.row];

cell.name -----------(name which is the uilabel in the tableviewcell class is not displayed ,even in the help it does not exist! ,I have no idea why )

The code below works by tagging the label

    // Configure the cell...

//    UILabel *label = (UILabel *)[cell viewWithTag:111];
//              label.text= [data objectAtIndex:indexPath.row];

My query is how do I make the outlet part work without tagging and why uilabel is not displayed in the tableviewCOntroller ?

Please help me out.I would really appreciate the help.

Thanks in Advance.

Upvotes: 0

Views: 1510

Answers (2)

LE SANG
LE SANG

Reputation: 11005

Remember to loadNib:

static NSString *CellIdentifier = @"CellIdentifier";
 MyCustomTableCell *cell = (MyCustomTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[MyCustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MyCustomTableCell" owner:self options:nil] lastObject];
       // UINib *theNib = [UINib nibWithNibName:@"MyCustomTableCell" bundle:nil];
       // cell = [[theNib instantiateWithOwner:self options:nil] lastObject];
    }

Upvotes: 3

andykkt
andykkt

Reputation: 1706

If your custom cell's class name is "MyCustomTableCell" which subclassing from UITableViewCell then code must look like this

static NSString *CellIdentifier = @"Cell";
MyCustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier   forIndexPath:indexPath];

if (cell == nil) {
    cell = [[MyCustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault
    reuseIdentifier:CellIdentifier ];
 }

then you can access it's property

cell.name = @"cell name";

Upvotes: 3

Related Questions