Ankit Sachan
Ankit Sachan

Reputation: 7840

unable to use custom cell

I am not able to make out whatrs wrong with this piece of code all I am trying to do is use custom table cell 'AlertCell' when device is in portrait mode and 'AlertCellLandScape' if device is in 'landscape' mode.

its saying cell is undeclared and inside the condition check its saying unused cell

static NSString *CellIdentifier1 = @"Cell1";
static NSString *CellIdentifier2 = @"Cell2";
UITableViewCell *cell;

if (isPortraitMode) 
{
    AlertsTableCell *cell = (AlertsTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
}

else {
    AlertsTableCellLandScape *cell = (AlertsTableCellLandScape *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
}






if (cell == nil) {
   // cell = [[[AlertsTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    if (isPortraitMode) {
        cell = [[[AlertsTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
    }
    else {
        cell = [[[AlertsTableCellLandScape alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2] autorelease];
    }
}

Please shed some light on this.

Thanks in advance

Upvotes: 0

Views: 389

Answers (3)

LuckyLuke
LuckyLuke

Reputation: 49047

You could do this instead, you have issues with how you declared the types of the cell, but this should work unless you are accessing the cell-reference outside if-else portrait block.

static NSString *CellIdentifier1 = @"Cell1";
static NSString *CellIdentifier2 = @"Cell2";

if (isPortraitMode) {
    AlertsTableCell *cell = (AlertsTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];

    if (cell == nil) {
        cell = [[[AlertsTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
    }

    return cell;

} else {
    AlertsTableCellLandScape *cell = (AlertsTableCellLandScape *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];

    if (cell == nil) {
        cell = [[[AlertsTableCellLandScape alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2] autorelease];
    }

    return cell;
}

Upvotes: 2

Stefan Ticu
Stefan Ticu

Reputation: 2103

Be sure to give it the correct identifier in xcode:

if (isPortraitMode) 
{
    AlertsTableCell *cell = (AlertsTableCell *)[tableView dequeueReusableCellWithIdentifier:@"AlertCell"];
}

else {
    AlertsTableCellLandScape *cell = (AlertsTableCellLandScape *)[tableView dequeueReusableCellWithIdentifier:@"AlertCellLandScape"];
}

and to set it correctly (fil the Identifier field) in IB for the respective cells

Upvotes: 0

Terry Wilcox
Terry Wilcox

Reputation: 9040

You have a scope issue. You're declaring cell inside your if/else, so it doesn't exist outside of them.

Declare cell before your if/else, then assign it inside.

Upvotes: 1

Related Questions