'UITableView dataSource必须从tableView返回一个单元格:cellForRowAtIndexPath:'

时间:2011-07-20 06:03:26

标签: iphone objective-c cocoa-touch uitableview ios4

我的代码是

//自定义表格视图单元格的外观。

- (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;
}

我不知道哪里出错,请帮助。

3 个答案:

答案 0 :(得分:5)

第一次调用dequeueReusableCellWithIdentifier:后,如果重用队列中没有可用的单元,则不检查nil并创建新的。

换句话说,如果重用队列中没有单元格,则您的请求类型不等于2且行不等于0或1,则不会创建您的单元格。这会导致您在控制台中看到异常。

答案 1 :(得分:0)

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

根据条件创建单元格时,您不需要在开始时编写它。

最后只返回零。因为我认为执行代码时必须有一些条件

答案 2 :(得分:0)

抱歉,我没有足够的评论来评论您的问题。你尝试过使用自定义单元吗? Custom Cell就像是对当前TableViewCells的修改。您几乎可以做任何TableViewCells不提供的事情。从我看来,你想要3个标签和1个图像。普通的TableViewCell不会为您提供3个标签的能力。除非你自己创造它。以下是您的链接。我希望这是你的想法。

iPhone Custom Cell Tutorial