Search code examples
iphoneobjective-ccocoa-touchuitableviewios4

'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'


my code is

// Customize the appearance of table view cells.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    ///   labels - names of Cities   ///


    UILabel *lblCity = [[UILabel alloc]initWithFrame:CGRectMake(15, 00, 200, 22)];
    lblCity.font = [UIFont systemFontOfSize:14];
    lblCity.backgroundColor = [UIColor clearColor];
    //lblCity.backgroundColor = [UIColor redColor];

    UILabel *lblDate = [[UILabel alloc]initWithFrame:CGRectMake(200, 00, 200, 22)]; 
    lblDate.font = [UIFont systemFontOfSize:14];
    lblDate.backgroundColor = [UIColor clearColor];
    //lblDate.backgroundColor = [UIColor redColor];

    UILabel *lblSchool = [[UILabel alloc]initWithFrame:CGRectMake(350, 00, 400, 22)];
    lblSchool.font = [UIFont systemFontOfSize:14];
    lblSchool.backgroundColor = [UIColor clearColor];
    //lblSchool.backgroundColor = [UIColor redColor];


    ///    Labels for description of city events    ///


    UILabel *lblEvent = [[UILabel alloc]initWithFrame:CGRectMake(15, 00, 200, 30)];
    lblEvent.font = [UIFont systemFontOfSize:12];
    lblEvent.backgroundColor = [UIColor clearColor];

    UILabel *lblEventAtDate = [[UILabel alloc]initWithFrame:CGRectMake(200, 00, 200, 30)];
    lblEventAtDate.font = [UIFont systemFontOfSize:12];
    lblEventAtDate.backgroundColor = [UIColor clearColor];

    UILabel *lblEventAtSchool = [[UILabel alloc]initWithFrame:CGRectMake(350, 00, 400, 30)];
    lblEventAtSchool.font = [UIFont systemFontOfSize:12];
    lblEventAtSchool.backgroundColor = [UIColor clearColor];



    if(RequestType == 2)
    {

        UIImageView *imgEventLabel = [[UIImageView alloc]initWithFrame:CGRectMake(00, 00, 480, 22)];

        UIView *viewDescription = [[UIView alloc]initWithFrame:CGRectMake(00, 00, 480, 35)];


        if(indexPath.row == 0)

        {
            static NSString *CellIdentifier = @"Cell11";

            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

                lblCity.text = @"City" ;
            //  [cell addSubview:lblCity];

                lblDate.text = @"Date" ;
            //  [cell addSubview:lblDate];

                lblSchool.text = @"School" ;
            //  [cell addSubview:lblSchool];

                imgEventLabel.image = [UIImage imageNamed:@"city_date_place.png"];
            //  [cell addSubview:imgEventLabel];



                [imgEventLabel addSubview:lblCity];
                [imgEventLabel addSubview:lblDate];
                [imgEventLabel addSubview:lblSchool];

                [cell.contentView addSubview:imgEventLabel];

            }

            return cell;
        }

        if(indexPath.row == 1)

        {
            static NSString *CellIdentifier = @"Cell12";

            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

                cell.selectionStyle=UITableViewCellSelectionStyleNone;
                cell.backgroundColor=[UIColor clearColor];
                cell.textLabel.numberOfLines = 999;


                lblEvent.text = @"Event in City";
                lblEventAtDate.text = @"Event on Date";
                lblEventAtSchool.text = @"Event at School";

                [viewDescription addSubview:lblEvent];
                [viewDescription addSubview:lblEventAtDate];
                [viewDescription addSubview:lblEventAtSchool];

                [cell.contentView addSubview:viewDescription];

            }

            return cell;


    }


    }

    // Configure the cell...

    return cell;
}

I don't know where is fault, Please Help.


Solution

  • After your first call of dequeueReusableCellWithIdentifier: You didn't check for nil and create a new one if there are no available cells in the reuse queue.

    In other words, if there are no cells in the reuse queue, your request type is not equal to 2, and row is not equal to 0 or 1, your cell will not be created. This causes an exception you see in the console.