【问题标题】:Tableview is not displaying any cells or rows content in new pushed viewControllerTableview 未在新推送的 viewController 中显示任何单元格或行内容
【发布时间】:2014-08-11 21:28:34
【问题描述】:

我不确定我错过了什么,但我正在尝试显示一个新的 viewController 并向它传递一些数据,但问题是当这个新的 viewController 被推送时,它不会显示单元格或行内容比如标签、文本字段等等。

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

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

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

更新

这就是问题发生的方式,当用户在当前视图控制器中选择一行时 didSelectRowAtIndexPath 调用一个新的视图控制器并推动这个控制器,但如果用户在使用屏幕中的键盘进行搜索时这样做是当问题发生时,新的 viewcontroller 推送不会在其 tableview 中显示任何内容..

已解决 在我的 cellForRowAtIndexPath

[cell setTranslatesAutoresizingMaskIntoConstraints:NO];

【问题讨论】:

  • 重新选择然后取消选择该行有什么意义?另外,popViewControllerAnimated: 然后 pushViewControllerAnimated: 有什么意义?我也希望这会导致错误,类似于不平衡的外观调用或类似的东西。
  • 对不起,我的错误这是我发现它工作的唯一方法..但它完全不正确,我不是故意发布它,只是修复它
  • 另外,删除单元格的所有子视图是什么?我认为您正在做大量工作以避免继承 UITableViewCell。看起来你有几个不同的布局,为每个布局创建一个 UITableViewCell 子类,让你自己的生活更轻松 =)
  • 感谢您的建议,但是,回到问题,这就是问题发生的方式,当用户在当前视图控制器中选择一行时,didSelectRowAtIndexPath 调用一个新的视图控制器并推送此控制器,但如果用户这样做在屏幕中使用键盘进行搜索时,会出现问题,即新的 viewcontroller 推送不会在其 tableview 中显示任何内容..有什么想法吗?
  • “在屏幕上用键盘搜索时”是什么意思?你在使用搜索栏和搜索结果控制器吗?

标签: ios objective-c uitableview ios7


【解决方案1】:

问题在于自动布局。我在 iOS 8 beta 上遇到了同样的问题。 (我知道你没有使用 i OS 8 但仍然)。

尝试将其关闭。应该解决问题。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-28
      • 2019-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      相关资源
      最近更新 更多