我的应用程序崩溃,原因'UITableView dataSource必须从tableView返回一个单元格:cellForRowAtIndexPath

时间:2015-06-08 11:45:41

标签: ios objective-c uitableview storyboard

我正在使用2个tableview。我正在使用这两个表使用标签。在Parent TableView中有3个单元格。并在Parent表的第3个单元格中添加另一个表。我仔细检查整个代码,但我找不到错误。但是我得到这个错误,我很困惑为什么?我不知道问题出在哪里。我的应用程序崩溃了。这是我的Crashi代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {   
        UITableViewCell *cell = nil;
        switch (tableView.tag)
        {
            case kTagBaseTableView:
            {
                if (indexPath.row == 0)
                {
                    static NSString *simpleTableIdentifier = @"Cell";

                    cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

                    if (cell == nil) {
                        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
                    }
                    cell.backgroundColor = [UIColor purpleColor];
                    //return cell;
                }
                if (indexPath.row == 1)
                {
                    static NSString *simpleTableIdentifier = @"Cell1";

                    cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

                    if (cell == nil) {
                        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
                    }
                    self.scrollView1 = (UIScrollView *)[cell viewWithTag:6];
                    self.customStoryboardPageControl = (TAPageControl *)[cell viewWithTag:7];

                    self.scrollView1 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, cell.frame.size.height+55)];
                    [self.scrollView1 setContentOffset:CGPointZero animated:NO];

                    self.scrollView1.delegate = self;

                    self.customStoryboardPageControl = [[TAPageControl alloc] init];
                    self.customStoryboardPageControl.frame = CGRectMake(10, 80, tableView.frame.size.width, 30);

                    self.customStoryboardPageControl.numberOfPages = self.imagesData.count;

                    [[cell contentView] addSubview:self.scrollView1];
                    [[cell contentView] addSubview:self.customStoryboardPageControl];

                    [self setupScrollViewImages];

                    cell.backgroundColor = [UIColor yellowColor];
                    //return cell;
                }
            }
            case kTagInnerTableView:
            {
                ORGContainerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ORGContainerCell"];
                cell.backgroundColor = [UIColor greenColor];
                [cell setCollectionData:self.sampleData];
                [cell setCollectionImage:self.sampleImage];
                cell.textLabel.text = @"Horizontal TableView";
                cell.backgroundColor = [UIColor redColor];
                //return cell;
            }
            default:
                break;
        }
        return cell;
    }

6 个答案:

答案 0 :(得分:3)

而不是ORGContainerCell *cell =,您应该使用cell =

答案 1 :(得分:0)

而不是UITableViewCell *cell = nil;

尝试UITableViewCell *cell = [[UITableViewCell alloc] init];

答案 2 :(得分:0)

错误消息已告诉您:有时,您没有返回UITableViewCell对象。

添加如下保护措施:

return cell ?: [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:nil];

(非标准?:运算符返回左侧,如果它是"真" [非零]或否则返回右侧)

答案 3 :(得分:0)

已编辑:

在您的kTagInnerTableView案例中,您已经使用了ORGContainerCell的新对象,因此您可以从ORGContainerCell的{​​{1}}返回case的本地对象{1}}。

只需使用最初声明的对象,如

switch-case

答案 4 :(得分:0)

父表视图中有3行,但您只为其中两个创建了单元格。您应该在第一个案例块中为第三个条件创建单元格代码

else if (indexPath.row == 2)

并在其中创建第二个表格视图。

答案 5 :(得分:0)

这是最终的工作代码。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier1 = @"Cell";
    static NSString *CellIdentifier2 = @"Cell1";
    static NSString *ORGContainerCellID1 = @"ORGContainerCell1";
    static NSString *ORGContainerCellID2 = @"ORGContainerCell2";
    static NSString *ORGContainerCellID3 = @"ORGContainerCell3";

    if (indexPath.section ==0)
    {
        UITableViewCell *cell; //= [[UITableViewCell alloc] init];
        if(indexPath.row == 0)
        {
            cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];

            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
            }
            btnSignIn = (UIButton *)[cell viewWithTag:2];
            lblUserName = (UILabel *)[cell viewWithTag:999];

            NSUserDefaults *defaults= [NSUserDefaults standardUserDefaults];
            if ([defaults boolForKey:@"hasUserLoggedIn"])
            {
                btnSignIn.hidden = TRUE;
                lblUserName.hidden = FALSE;
                lblUserName.text = [NSString stringWithFormat:@"Hello,%@",[[NSUserDefaults standardUserDefaults] stringForKey:@"userName"]];
            }
            else
            {
                btnSignIn.hidden = FALSE;
                lblUserName.hidden = TRUE;
            }
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            return cell;
        }
        else
        {
             cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];

            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
            }
            cell.separatorInset = UIEdgeInsetsMake(0, 10000, 0, 0);
            self.scrollView1 = (UIScrollView *)[cell viewWithTag:6];
            self.customStoryboardPageControl = (TAPageControl *)[cell viewWithTag:7];
            self.scrollView1.delegate = self;


            self.customStoryboardPageControl.numberOfPages = arrBannerData.count;

            [[cell contentView] addSubview:self.scrollView1];

            [self setupScrollViewImages];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        }
        return cell;
    }
    else
    {
        if(indexPath.row == 0)
        {
            ORGContainerCell *cell = [[ORGContainerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ORGContainerCellID1];
            if (cell == nil)
            {
                cell = [[ORGContainerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ORGContainerCellID1] ;
            }
            [cell setCollectionData1:self.arrProductName];
            [cell setCollectionImage1:self.arrProductImage];
            [cell setCollectionPrice1:arrProductPrice];
            [cell setCollectionHeader1:arrHeaderTitle];

            return  cell;
        }
        else if(indexPath.row == 1)
        {
            ORGContainerCell *cell = [[ORGContainerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ORGContainerCellID2];
            if (cell == nil)
            {
                cell = [[ORGContainerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ORGContainerCellID2] ;
            }
            [cell setCollectionData1:self.arrProductName1];
            [cell setCollectionImage1:self.arrProductImage1];
            [cell setCollectionPrice1:self.arrProductPrice1];
            [cell setCollectionHeader1:self.arrHeaderTitl1];

            return  cell;
        }
        else
        {
            ORGContainerCell *cell = [[ORGContainerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ORGContainerCellID3];
            if (cell == nil)
            {
                cell = [[ORGContainerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ORGContainerCellID3] ;
            }
            [cell setCollectionData1:self.arrProductName2];
            [cell setCollectionImage1:self.arrProductImage2];
            [cell setCollectionPrice1:self.arrProductPrice2];
            [cell setCollectionHeader1:self.arrHeaderTitl2];
            return  cell;
        }
    }
}