Reputation: 557
I am just, 1) Dequeueing the cell 2) Checking nil 3) setting cell data as per situation and returning the cell.
What is wrong with this code? What am i doing wrong? Simply checking nil
.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
GSDischargeListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
[tableView registerClass:[GSDischargeListCell class] forCellReuseIdentifier:cellIdentifier];
}
if (_searchController.active && ![_searchController.searchBar.text isEqual: @""] ) {
GSDischargePatient *patient = searchResultsArray[indexPath.row];
[cell setCellData:patient];
} else {
GSDischargePatient *patient = datasourceArray[indexPath.row];
[cell setCellData:patient];
}
_totalHeight = [cell estimatedHeightOfCell];
return cell;
}
Upvotes: 1
Views: 735
Reputation: 285069
First of all never register cells in cellForRowAtIndexPath
. Register your cell (once) in viewDidLoad
if necessary at all.
The error occurs because you are only registering a cell but not creating one if cell
is nil.
A more convenient way is to use the other dequeueReusableCellWithIdentifier
method (dequeueReusableCellWithIdentifier:forIndexPath:
) which returns always a non-null valid cell. A check for nil
is not needed. Further cast the cell to the subclass.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
GSDischargeListCell *cell = (GSDischargeListCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath: indexPath];
if (_searchController.active && ...
}
Upvotes: 2