MKDev MKDev - 3 months ago 13
Objective-C Question

UITableView Custom Cells Not Showing Up

I have a UITableViewController with two IBOutlet's to two different UITableViewCell's that were made in IB. However they are not showing up in the tableView. If you can find the error, I will really appreciate it.


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *categoryIdentifier = @"Category";
static NSString *songDetailsCellIdentifier = @"sdci";
static NSString *socialCellIdentifier = @"sdcssi";
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
switch (indexPath.row) {
case 0:
//imageView = [[UIImage alloc] initWithData:imageData];
//songNameLabel.text = songName;
//artistNameLabel.text = artistName;
//albumNameLabel.text = albumName;
//numberInChartsLabel.text = [[NSString alloc] initWithFormat:@"%d", numberInCharts];
break;
case 1:
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:categoryIdentifier] autorelease];
cell.textLabel.text = [[NSString alloc] initWithFormat:@"Category"];
cell.detailTextLabel.text = [[NSString alloc] initWithFormat:@"%@", category];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break;
case 2:
cell.textLabel.text = [[NSString alloc] initWithFormat:@"Preview This Track"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break;
case 3:
cell.textLabel.text = [[NSString alloc] initWithFormat:@"View This Song In iTunes"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break;
case 4:
cell.textLabel.text = [[NSString alloc] initWithFormat:@"View Artist In iTunes"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break;
case 5:
break;
}
// Configure the cell...
if (indexPath.row == 0) {
songDetailsCell = [[[UITableViewCell alloc] initWithFrame:songDetailsCell.frame reuseIdentifier:songDetailsCellIdentifier] autorelease];
return songDetailsCell;
}
if (indexPath.row == 5) {
socialCell = [[[UITableViewCell alloc] initWithFrame:socialCell.frame reuseIdentifier:socialCellIdentifier] autorelease];
return socialCell;



}
else if (indexPath.row >= 1 && indexPath.row <=5)
{
return cell;
}
return nil;
}

Answer

Put the switch in an if{indexPath.section} statetment, including the UITableViewCell *cell...

Then the rest below that needs to be indexPath.section, instead of indexPath.row

if(indexPath.section==0) {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    switch (indexPath.row) {
        case 0:

....


 if (indexPath.section == 1) {
    songDetailsCell = [[[UITableViewCell alloc] initWithFrame:songDetailsCell.frame reuseIdentifier:songDetailsCellIdentifier] autorelease];
    return songDetailsCell;
}


....

Follow that format.

Comments