【问题标题】:Unable to get table to load in UIView无法在 UIView 中加载表
【发布时间】:2016-04-16 04:23:46
【问题描述】:

我有一个 ViewController,我在其中放置了另一个小的 UIView,其中我放置了一个 TableView,但不确定如何在此表中显示数组中的数据,任何帮助将不胜感激。我正在尝试使用以下代码加载表格:

-(void) animateResults {


_resultsLabel.text = [NSString stringWithFormat:@"You Scored %d", runningScore ];
resultsTable.delegate = self;
resultsTable.dataSource = self;
[self.resultsTable registerClass:[resultsViewCell self] forCellReuseIdentifier:@"resultsListCell"];
[self.resultsTable reloadData];
[self.view layoutIfNeeded];
[UIView animateWithDuration:0.3 animations:^{
    _resultsView.frame = self.view.frame;


} completion:^(BOOL finished){

   NSLog(@"%@", questionsArray);

}];


}

我在 TableView 中使用自定义单元来加载 NSMutableArray。我已经尝试将此代码用于表格视图:

-(NSInteger)numberOfSectionsInTableView:(resultsViewCell *)tableView {
//Return number of sections
return 1;

}

//get number of rows by counting number of challenges
-(NSInteger)tableView:(resultsViewCell *)tableView numberOfRowsInSection:(NSInteger)section {
return questionsArray.count;
}


//setup cells in tableView
-(resultsViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//setup cell
static NSString *CellIdentifier = @"resultsListCell";

resultsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

NSDictionary *results = [questionsArray objectAtIndex:indexPath.row];

NSString *resultsName = [results objectForKey:@"answers"];

BOOL correct = [[results objectForKey:@"correct"] boolValue];

if (!correct) {
    cell.resultsIcon.image = [UIImage imageNamed:@"BlackIconLock.png"];
}

else{
    cell.resultsIcon.image = nil;
}

cell.resultsName.text = resultsName;

return cell;
}

但 _resultsView 不加载不知道为什么。到目前为止,我得到了很多很大的帮助,我真的很感激。我已经在这一点上停留了大约 2 周。请帮忙!

【问题讨论】:

  • 检查_resultView是否为nil
  • 抱歉,我刚开始学习这项学习,我该如何检查?再次为noobness道歉
  • animateResults 的开头添加NSLog(@"resultsView: %@", _resultsView) 行,运行应用程序并查看控制台输出。如果它打印resultsView: (null),则视图为零。
  • 您必须先初始化视图才能使用它。

标签: ios objective-c uitableview uiview


【解决方案1】:

如果你使用上面的xcode 7,你会在storyboard中使用UIContainerView中间到ViewController,它会自动将UITableView嵌入到containerView中,例如

【讨论】:

    【解决方案2】:

    尝试在completion block 中重新加载animateResults 中的数据。像这样的,

    completion:^(BOOL finished){
    
         NSLog(@"%@", questionsArray);
         [self.resultsTable reloadData];
    
    }];
    

    【讨论】:

    • 我这样做并收到此错误 -[UITableViewCell resultsIcon]: unrecognized selector sent to instance
    • 您是从界面生成器设置 tableview 类吗?如果是,那你为什么要在 animateResult 方法中注册类?
    • 所以删除 [self.resultsTable registerClass:[resultsViewCell self] forCellReuseIdentifier:@"resultsListCell"];?如果我这样做,那么我会在 Interface Builder 文件中得到 Unknown class resultsView。在我的调试开始时。现在还收到一个 UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance
    【解决方案3】:

    我终于!明白了我的意图是在 UIView 中加载 UITableView。我试图将 TableView 委托和数据源拖放到 ViewController 上,但每次加载应用程序时它都会崩溃。所以我把表格视图委托和数据源移到了这里的 UIView 加载方法中:我尝试使用这个方法来这样做。

     -(void) animateResults {
     //this part of my method actually worked it displayed the final score from a predifined variable.
     _resultsLabel.text = [NSString stringWithFormat:@"You Scored %d", runningScore ];
     //i tried using this to set my UITableView delegate & dataSource when the method was initiated instead of UIViewController load.
     resultsTable.delegate = self;
     resultsTable.dataSource = self;
    //used this code to register UIViewCell that was nonexistent, 
    //i declared *CellIdentifier = @"resultsListCell" and it should have 
    //been *CellIdentifier = @"resultsCell". so i received an error used this code to solve it.
    //duh. cell don't exist must not be the cell you created.My Bad.
     [self.resultsTable registerClass:[resultsViewCell self]   forCellReuseIdentifier:@"resultsListCell"];
     //this code worked but after debugging i found out there was no data to load.
     [self.resultsTable reloadData];
     //was desperate at this point not sure why i put this in.
     [self.view layoutIfNeeded];
     //this also worked loads my UIView.
     [UIView animateWithDuration:0.3 animations:^{
    _resultsView.frame = self.view.frame;
    
    } completion:^(BOOL finished){
    
    NSLog(@"%@", questionsArray);
    
    }];
    
    }
    

    所以我的解决方案因简单而令人沮丧。当我尝试拖放数据源和委托时,我的应用程序崩溃的原因是错误标记了“静态 NSString *CellIdentifier”,因此请确保您的单元格标识符是正确的!不知道这一点,我试图在上述方法中加载 UITableView 数据源和委托。此后,我已将上述方法修改为此。

    -(void) animateResults {
    
    //populate label
    _resultsLabel.text = [NSString stringWithFormat:@"You Scored %d", runningScore   ];
    
    //reload table
    [self.resultsTable reloadData];
    
    //Load View
    [UIView animateWithDuration:0.3 animations:^{
    
        _resultsView.frame = self.view.frame;
    
    } completion:^(BOOL finished){
    
        NSLog(@"%@", questionsArray);
    
    }];
    }
    

    这个精简的代码有效,因为我连接了数据源并委托给 UIViewController。这样做会在 UIViewController 加载时加载表格,因此无需以编程方式执行此操作。此外,纠正错误标记的单元格标识符消除了对 registerClass 变量的需要。希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-10
      • 1970-01-01
      • 2015-02-13
      • 1970-01-01
      • 2013-12-07
      • 1970-01-01
      相关资源
      最近更新 更多