Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell with xib

I'm developing an app for iOS which use the xib files.

Usually I use Storyboard and I don't know how to set up a UITableViewCell with xib files. When I make a xib file with a UITableView, I see a table with some row, now I need to edit this row to write what I stored in an array.

How can I use the xib file to design a UITableViewCell?

I need to do a very simple table: I would to use the Basic preset for the cell to display a title. I know that I've to connect the delegate and DataSource to the file owner for table view and I put in the file owner the UITableViewDelegate and UITableViewDataSource.

Now how I can edit the content of the cell? I found on the web some guide that tells to create a xib file with a UITableViewCell and I did it, but I don't know how to work with this

like image 655
lucgian84 Avatar asked Oct 16 '13 09:10

lucgian84


People also ask

How do I load a Tableview cell in Xib?

h, a new subclass of UITableViewCell and add IBOutlets for the components you want. Then create a new "Empty XIB" file. Open the Xib file in IB, add a UITableViewCell object, set its identifier to "MyCellIdentifier", and set its class to MyCell and add your components. Finally, connect the IBOutlets to the components.


1 Answers

Firstly, you need to create the class for the customCell which inherits from UITableViewCell. Now, add the properties you want to have in your customCell. In this example I added cellImage and cellLabel.

@property (nonatomic, strong) IBOutlet UILabel *cellLabel;
@property (nonatomic, strong) IBOutlet UIImageView *cellImageView;

After that you need to link the UILabel and the UIImageView from the CustomCell to the Nib.

You need to add:

- (void)viewDidLoad 
{
    ....
    [self.tableView registerNib:[UINib nibWithNibName:@"xibName" bundle:nil] forCellReuseIdentifier:CellIdentifier];
    .....
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CustomCellReuse";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // Configure the cell...
    [cell.cellImageView setImage:[UIImage imageNamed:@"whatever"]];
    [cell.cellLabel setText = @"whatever"];
    return cell;
}
like image 105
Alex Avatar answered Nov 10 '22 08:11

Alex