user3745888
user3745888

Reputation: 6253

How can I initialize a UITableView added to a .xib in objective c?

I'm adding a xib into my view controller like:

MyControllerOfXib *myCOntrolXib = [[MyControllerOfXib alloc] initWithFrame:CGRectMake(width, height)];

In this xib I have a label and a UItableView. The UIlabel is easy to add text:

myCOntrolXib.label.text = @"someText";

But how can I initialize the tableView? What need I then, Could I use

 myCOntrolXib.table.delegate = self;

Without problems?

Upvotes: 2

Views: 751

Answers (3)

shubham shukla
shubham shukla

Reputation: 1

you can use a custom table view cell to populate your table view with elements. A custom xib would be associated with the tableViewCell and write the following code in the cellForRowAtIndexPath to initialize the cell.

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

{

static NSString *simpleTableIdentifier = @"SimpleTableCell";

SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) 
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
} 

cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];
cell.thumbnailImageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
cell.prepTimeLabel.text = [prepTime objectAtIndex:indexPath.row];

return cell;

}

simpleTableCell is the name of the custom TableViewCell class.

Refer To This Link If you Face Any Issue http://www.appcoda.com/customize-table-view-cells-for-uitableview/

Upvotes: 0

Paul Razvan Berg
Paul Razvan Berg

Reputation: 21420

Simply do the following:

CustomView *view = [[[NSBundle mainBundle] loadNibNamed: @"YourNibName" owner: nil options: nil] firstObject];

Then, don't forget to add the view to the main view using the addSubview method. Anyway, now you can do:

[view.customProperty doSomething];

Upvotes: 1

user5890979
user5890979

Reputation:

Initialize your UITableView from the initWithFrame method in your subclass.

After that you can set the delegate without problems on your mainViewController.

EDIT: You can call "reload" data etc on your XIB UITableView as usual.

Upvotes: 1

Related Questions