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
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With