I am trying to display two different UITableViewCells I have made in InterfaceBuilder, however I am receiving this error.
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
I have create two new nib's I have changed the file owners class to the UITableViewController I am trying to display them in, and hooked them up using IBOutlets etc.
The error happens inside TableView:cellForRowAtIndexPath and this is what my method for that class looks like
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if(indexPath.section == 0) {
// Manufactures --->
if (indexPath.row == 0) {
cell = nil;
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"CodeSearchTextCell" owner:self options:nil];
cell = codeSearchTextCell; //Loads tableviewcells with custome xib-cell that I have made in Interface builder
self.codeSearchTextCell = nil;
}
codeTextField.layer.borderColor = [UIColor lightGrayColor].CGColor;
codeTextField.layer.borderWidth = 0.5f;
cell.userInteractionEnabled = NO;
}
else if (indexPath.row == 1) {
cell = nil;
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"CodeSearchCell" owner:self options:nil];
cell = codeSearchCell; //Loads tableviewcells with custome xib-cell that I have made in Interface builder
self.codeSearchCell = nil;
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //adds disclosure indicator
cell.userInteractionEnabled = YES;
}
}
return cell;
}
I am not sure what I am doing wrong but can see clearly when putting the break point on the last line where I return the cell its coming back with no value.
any help would be greatly appreciated.
In this example, I assume you have a UIView
subclass named CodeSearchCell
:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
if (0 == indexPath.section) {
if (0 == indexPath.row) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CodeSearchTextCell" owner:self options:nil];
CodeSearchTextCell *codeSearchTextCell = [tableView dequeueReusableCellWithIdentifier:@"CodeSearchCell"];
if ( nil == codeSearchTextCell ) {
codeSearchTextCell = (CodeSearchTextCell *)[nib objectAtIndex:0];
}
cell = codeSearchTextCell;
} else if (1 == indexPath.row) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CodeSearchCell" owner:self options:nil];
CodeSearchCell *codeSearchCell = [tableView dequeueReusableCellWithIdentifier:@"CodeSearchCell"];
if ( nil == codeSearchCell ) {
codeSearchCell = (CodeSearchCell *)[nib objectAtIndex:0];
}
cell = codeSearchCell;
} else {
// If indexPath.row > 1
// Add logic to make sure cell isn't nil.
} else {
// If indexPath.section > 0
// Add logic to make sure cell isn't nil.
}
return cell;
}
i think you should try it this way hope it will help you
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if(indexPath.section == 0) {
// Manufactures --->
if (indexPath.row == 0) {
cell = nil;
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"CodeSearchTextCell" owner:self options:nil] objectAtIndex:0];
}
codeTextField.layer.borderColor = [UIColor lightGrayColor].CGColor;
codeTextField.layer.borderWidth = 0.5f;
cell.userInteractionEnabled = NO;
}
else if (indexPath.row == 1) {
cell = nil;
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"CodeSearchCell" owner:self options:nil] objectAtIndex:0];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //adds disclosure indicator
cell.userInteractionEnabled = YES;
}
}
return cell;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.