Andy_24
Andy_24

Reputation: 1493

How to assign different identifiers for different cell styles in one uitableview?

I'm a freshman in ios development, I want to implement different cell styles in different sections in one uitableview. And my tableview have two sections, each section have three rows.

I know different cell should have different reuseidentifier to retrieve reuse cell, but when I compile my app, the console always shows the below information:

2012-08-26 14:04:45.571 Defferent Cell Styles[703:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

Any help would be highly appreciated, my code is below:

@end

@implement LGViewController

@synthesize data = _data;
@synthesize list = _list;

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray *dataArray = [[NSArray alloc] initWithObjects:@"fwfwf", @"ffgfg", @"sfsfsf", nil];
    NSArray *listArray = [[NSArray alloc] initWithObjects:@"fdff", @"ffdfsw", @"eergerg", nil];
    self.data = dataArray;
    self.list = listArray;
    self.navigationItem.title = @"Data";
}

#pragma mark - DataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return [_data count];
    }
    else if (section == 1){
        return [_list count];
    }
    return section;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *accessoryIdentifier = @"Cells";
    static NSString *switchIdenfier = @"Cells";
    static NSString *sliderIdentifier = @"Cells";

    UITableViewCell *cell;   
    if ((indexPath.section == 0 && indexPath.section == 1) && indexPath.row == 0)
    {
        cell = [tableView dequeueReusableCellWithIdentifier:accessoryIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:accessoryIdentifier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


    }
    else if ((indexPath.section == 0 && indexPath.section ==1) && indexPath.row == 1)
    {
        cell = [tableView dequeueReusableCellWithIdentifier:switchIdenfier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:switchIdenfier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UISwitch *soundSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
        [soundSwitch addTarget:self action:@selector(showAlert) forControlEvents:UIControlEventValueChanged];
        [soundSwitch setOn:NO animated:NO];
        cell.accessoryView = soundSwitch;


    }
    else if ((indexPath.section ==0 && indexPath.section == 1) && indexPath.row == 2)
    {
        cell = [tableView dequeueReusableCellWithIdentifier:sliderIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sliderIdentifier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, 60, 20)];
        cell.accessoryView = slider;


    }

    return cell;   
}

Upvotes: 2

Views: 2313

Answers (2)

parikhnirav
parikhnirav

Reputation: 33

Better way to configure cell

 if (indexPath.section == 0) {
    switch (indexPath.row) {
        case 0:
            //configure you cell here..
            break;

        case 1:
            //configure you cell here..
            break;

        case 2:
            //configure you cell here..
            break;

        default:
            break;
    }
} else if (indexPath.section == 1) {
    switch (indexPath.row) {
        case 0:
            //configure you cell here..
            break;

        case 1:
            //configure you cell here..   
             break;

        case 2:
            //configure you cell here..                
            break;

        default:
            break;
    }
} else if (indexPath.section == 2) {
    switch (indexPath.row) {
        case 0:
        //configure you cell here..                
        break;

        case 1:
        //configure you cell here..                
        break;

        default:
            break;
    }
} 
}
return cell;

Upvotes: 2

jrturton
jrturton

Reputation: 119262

You'll never return a cell because indexPath.section can never be 0 and 1 at the same time:

else if ((indexPath.section ==0 && indexPath.section == 1) && indexPath.row == 2)

Should be

else if ((indexPath.section ==0 || indexPath.section == 1) && indexPath.row == 2)

In all cases. || is OR, && is AND.

Though really, you could just drop the check on section, since it will always either be 0 or 1.

Upvotes: 2

Related Questions