Tableview未在新推送的viewController中显示任何单元格或行内容

时间:2014-08-11 01:49:04

标签: ios objective-c uitableview ios7

我不确定我错过了什么,但我试图显示一个新的viewController并将一些数据传递给它,但问题是当这个新的viewController被推送时它没有&#39 ; t显示单元格或行内容,如标签,文本字段等。

这就是我从一个控制器推送到另一个控制器的方式..

    - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        BillComparatorViewController *vc = [[BillComparatorViewController alloc] init];
        vc.selectedInstitution = [self.myInstitution objectAtIndex:indexPath.row];
        vc.managedObjectContext = self.managedObjectContext;

        [self.view endEditing:YES];

        [self.navigationController pushViewController:vc animated:YES];

    }

然后在我的新推视图控制器中,这是我的cellForRowAtIndexPath函数..

//UITableView Delegate + Datasource
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }

    for (UIView *view in cell.contentView.subviews)
        [view removeFromSuperview];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];

    cell.textLabel.text = [self.labels objectAtIndex:indexPath.row];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = [self.labels objectAtIndex:indexPath.row];

    CGFloat i = [[self.rowHeights objectAtIndex:indexPath.row] floatValue];

    UILabel *cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 280, 15)];
    cellLabel.font = [UIFont fontWithName:kFONTNAME size:kSystemFontSizeRows];


    if ([[self.tableViewElements objectAtIndex:indexPath.row] isKindOfClass:[UIButton class]]){

        cell.textLabel.font = [UIFont fontWithName:kFONTNAME size:kSystemFontSizeRows];


        cell.backgroundColor = [UIColor lightGrayColor];
        [cell.contentView addSubview:[self.tableViewElements objectAtIndex:indexPath.row]];
        [cell.contentView addSubview:arrow];

    }
    else if (i > kTableRowHeight)
    {
        cell.textLabel.text = nil;
        UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, kElementWidth/2, 75)];
        lbl.text = [self.labels objectAtIndex:indexPath.row];
        lbl.font = _myFont;
        lbl.numberOfLines = 0;
        lbl.lineBreakMode = NSLineBreakByWordWrapping;
        lbl.backgroundColor = [UIColor clearColor];

        [cell.contentView addSubview:[self.tableViewElements objectAtIndex:indexPath.row]];
        [cell.contentView addSubview:lbl];

        if ([[self.tableViewElements objectAtIndex:indexPath.row] isKindOfClass:[UISwitch class]])
        {
            [cell.contentView addSubview:[self.tableViewElements objectAtIndex:indexPath.row]];
        }

        if ([[self.tableViewElements objectAtIndex:indexPath.row] isKindOfClass:[UITextField class]])
        {
            // Can make a class for this cell's contentview, however it never repeats in code
            UITextField* txtField = [self.tableViewElements objectAtIndex:indexPath.row];
            txtField.textColor = kBlueColor;
            txtField.textAlignment = NSTextAlignmentLeft;
            txtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; // iOS 6 support

            NSString *imageName = @"image";
            NSString *arrowName = @"image1";
            if (selectedRow == indexPath.row)
            {
                imageName = @"image";
                arrowName = @"image1";
                txtField.textColor = [UIColor whiteColor];
            }

            UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(2*kElementWidth/3 -12, 23, 135, 27)];
            imgView.image = [UIImage imageNamed:imageName];
            [cell.contentView addSubview:imgView];

            UIImageView *arrowView = [[UIImageView alloc] initWithFrame:CGRectMake(kElementWidth+10, 31, 8.5, 10)];
            arrowView.image = [UIImage imageNamed:arrowName];
            [cell.contentView addSubview:arrowView];

            [cell.contentView addSubview:txtField];
        }
    }
    else
    {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.text = [self.labels objectAtIndex:indexPath.row];
        cell.textLabel.font = _myFontSmall;
        cell.textLabel.textColor = kBlueColor;
        cell.textLabel.backgroundColor = [UIColor clearColor];

        if ([[self.tableViewElements objectAtIndex:indexPath.row] isKindOfClass:[UITextField class]])
        {
            // Can make a class for this cell's contentview, however it never repeats in code
            UITextField* txtField = [self.tableViewElements objectAtIndex:indexPath.row];
            txtField.textColor = kBlueColor;
            txtField.textAlignment = NSTextAlignmentLeft;
            txtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; // iOS 6 support

            NSString *imageName = @"image";
            NSString *arrowName = @"image1";
            if (selectedRow == indexPath.row)
            {
                imageName = @"image";
                arrowName = @"image2";
                txtField.textColor = [UIColor whiteColor];
            }

            UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(2*kElementWidth/3 -12, 12, 135, 27)];
            imgView.image = [UIImage imageNamed:imageName];
            [cell.contentView addSubview:imgView];

            UIImageView *arrowView = [[UIImageView alloc] initWithFrame:CGRectMake(kElementWidth+10, 20, 8.5, 10)];
            arrowView.image = [UIImage imageNamed:arrowName];
            [cell.contentView addSubview:arrowView];

            [cell.contentView addSubview:txtField];
        }

        if (isSearching)
        {
            cellLabel.text = [searchElements objectAtIndex:indexPath.row];
            [cell.contentView addSubview:cellLabel];
        }

    }

    return cell;
}

有点长的抱歉,但我认为发布它很重要。

更新

当问题发生时,用户在当前viewcontroller中选择一行 didSelectRowAtIndexPath 调用新的viewcontroller并推送此控制器,但如果用户在屏幕上使用键盘搜索时执行此操作当问题发生时,新的viewcontroller push doenst在其tableview中显示任何内容..

解决 在我的 cellForRowAtIndexPath

[cell setTranslatesAutoresizingMaskIntoConstraints:NO];

2 个答案:

答案 0 :(得分:0)

我认为问题是启动BillComparatorViewController的对象。 你喜欢这样。你可以得到解决方案

BillComparatorViewController *vc = [[BillComparatorViewController alloc] initWithNibName:"BillComparatorViewController" Bundle:nil];

答案 1 :(得分:0)

问题出在Autolayout上。 iOS 8 beta也遇到了同样的问题。 (我知道你不是在使用i OS 8,但仍然如此)。

尝试将其关闭。应该排除问题。