Reputation: 67
I Can't load uitableviewcell which I took as .xib with tableviewcell class. Please help me out thanks in advance.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellId = @"itemCell";
NSDictionary *dictDetailList = [_arrItemList objectAtIndex:indexPath.row];
HomeRestuTableViewCell *cell = (HomeRestuTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellId];
if(!cell) {
[tableView registerNib:[UINib nibWithNibName:@"itemCell" bundle:nil] forCellReuseIdentifier:cellId];
cell = (HomeRestuTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellId];
}
[cell setupHomeRestuCell:dictDetailList];
return cell;
}
Upvotes: 0
Views: 146
Reputation: 1136
I think you are confused with Cell Identifier and Register nib for Tableviewcell
Register Nib is used to allocate your xib to your cell in
[tableView registerNib:[UINib nibWithNibName:@"HomeRestuTableViewCell" bundle:nil] forCellReuseIdentifier:cellId];
So It is preferred to use this line in ViewDidLoad of your viewController
and CellIdentifier is used identify the type of the cell, as in Table View there can be multiple types of cell, so to identify them, TableView used CellReuseIdentifier
Once you register cell for your table View, You can use the following method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellId = @"itemCell";
NSDictionary *dictDetailList = [_arrItemList objectAtIndex:indexPath.row];
HomeRestuTableViewCell *cell = (HomeRestuTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellId];
if(!cell) {
cell = [[HomeRestuTableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: cellId];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
[cell setupHomeRestuCell:dictDetailList];
return cell;
Hope that solves your problem.
Upvotes: 1
Reputation: 3898
Add this line in viewDidLoad
Make sure reuse identifier same both place and you should have itemCell.xib in your bundle
[tableView registerNib:[UINib nibWithNibName:@"itemCell" bundle:nil] forCellReuseIdentifier:@"itemCell"];
Upvotes: 1
Reputation: 3947
You've used the nib name itemCell, which is probably wrong. Typically you'll name your nib the same as the class which backs it, which means you need to load that named nib, i.e.
[tableView registerNib:[UINib nibWithNibName:@"HomeRestuTableViewCell" bundle:nil] forCellReuseIdentifier:cellId];
There's no requirement that you name nibs the same as the class, so check what the filename is for your nib, but if it's HomeRestuTableViewCell.xib
, and it's in your main bundle, the above registration should fix the error for you.
Upvotes: 0