【问题标题】:Nested UITableViewController does not show any cells嵌套的 UITableViewController 不显示任何单元格
【发布时间】:2014-08-27 18:35:14
【问题描述】:

我已使用 Apple 文档中的方法将 UITableViewController 添加到我的主视图控制器:

- (void)viewWillAppear:(BOOL)animated {
    myTableViewController = [MyTableViewController new];
    [self displayContentController:myTableViewController];
}

- (void)displayContentController:(UIViewController *)viewController; {
    [self addChildViewController:viewController];
    viewController.view.frame = self.view.bounds;
    [self.view addSubview:viewController.view];
    [viewController didMoveToParentViewController:self];
}

我想在这个控制器中显示一些示例单元格:

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];

(我认为我不需要更多配置,因为它是 UITableViewController,而不是 UITableView)

 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.textLabel.text = @"Under development";
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

但我只能看到空单元格(我还尝试更改背景颜色以确保它不仅仅是 while 标签颜色):

几个小时后,我不知道自己做错了什么。你知道我错过了什么吗?

【问题讨论】:

  • 您是否将委托和数据源设置为 tableview ?
  • 我认为我不需要这样做,因为它是 UITableViewController,而不是 UIViewController 中的 UITableView。

标签: ios iphone uitableview ios7 uiviewcontroller


【解决方案1】:

你必须使用它:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(!cell)
    {
        cell=[[UITableViewCell alloc]init];
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.text = @"Under development";
    return cell;
}

【讨论】:

  • 我不需要这样做,因为我之前已经注册了一个单元格标识符,这永远不会为零(也在调试器中检查)。
【解决方案2】:

试试看:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return Table_Arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"subwaysurfer"];
    if(!cell)
    {
        cell=[[UITableViewCell alloc]init];
    }
     UIImageView *imgg;
    UILabel *txt;
    UIImageView *Acc_img;




    imgg=[[UIImageView alloc]initWithFrame:CGRectMake(10,5, 50, 50)];

    imgg.image = [UIImage imageNamed:[imageArr objectAtIndex:indexPath.row]];




    cell.highlighted=YES;
    cell.backgroundColor=[UIColor clearColor];
    txt=[[UILabel alloc]initWithFrame:CGRectMake(20, 0, self.view.frame.size.width-80, 60)];
    txt.numberOfLines = 3;
    txt.text=[Table_Arr objectAtIndex:indexPath.row];
    txt.backgroundColor=[UIColor clearColor];
   // txt.textColor=[UIColor colorWithRed:66.0/255.0 green:51.0/255.0 blue:57.0/255.0 alpha:1.0];


    txt.textColor=[UIColor colorWithRed:44.f/255.f green:78.f/255.f blue:134.f/255.f alpha:1];


    txt.font=[UIFont fontWithName:@"AvenirNextCondensed-Medium" size:16.0];

    Acc_img=[[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width-40, 20, 20, 20)];
    Acc_img.image=[UIImage imageNamed:@"arrow.png"];

    if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad){
        imgg.frame = CGRectMake(10, 5, 80, 80);
        txt.frame = CGRectMake(120, 10, self.view.frame.size.width-160, 70);
        Acc_img.frame = CGRectMake(self.view.frame.size.width-80, 25, 40, 40);
        txt.font=[UIFont systemFontOfSize:28.0];

    }


    [cell addSubview:imgg];

    [cell addSubview:txt];

    [cell.contentView addSubview:Acc_img];


    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{



}


// fixed font style. use custom view (UILabel) if you want something different

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad){
        return 90;
    }
    else{
        return 60;
    }


}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0)];
}

【讨论】:

    【解决方案3】:

    好的,问题解决了。看来,即使使用具有内置属性 tableView 的 UITableViewController 也有必要初始化 tableView (或者至少它确实帮助了我)。

    self.tableView = [UITableView new];
    

    【讨论】:

    • 不,你做错了什么。我猜你的 MyTableViewController 不是 UITableViewController 子类,或者如果你有 tableview 控制器的 xib,tableview 出口没有设置。
    • 嗯,有可能。我仔细检查了 - 我的类是 UITableViewController 子类,我没有使用 Xibs 和 Storyboards - 我正在以编程方式创建所有内容。
    • 我确定的一件事是它不是嵌套错误 - 即使在退出嵌套之后我仍然遇到同样的问题。
    • 我遵循了类似的层次结构,并且单元格显示良好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多