I have designed a custom UITableViewCell in the same nib file as my UITableView. I am then showing it using the following code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"SearchViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSInteger section = [indexPath section];
switch (section) {
case 0: // First cell in section 1
return self.priceRangeCell;
break;
default:
// Do something else here if a cell other than 1,2,3 or 4 is requested
return cell;
break;
}
My question is that I am confused on the whole dequeueReusableCellWithIdentifier business. Do I even need the following part ??:
static NSString *CellIdentifier = @"SearchViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Can you see any other problems with my code?
Thanks
You don’t absolutely have to use the code you mentioned, but it would be bad form not to. If you don’t use that if clause, you will be creating new cells every time you scroll your table instead of reusing old ones — that’s not good for memory usage.
Secondly, there is something missing from your code — you don’t populate your cell with anything. You return a cell, but you haven’t added any content to it.