无法在UIView中加载表

时间:2016-04-16 04:23:47

标签: ios objective-c uitableview uiview

我有一个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周了。请帮忙!

3 个答案:

答案 0 :(得分:0)

如果你使用上面的xcode 7,你将在故事板中使用UIContainerView middle到ViewController,它会自动将UITableView嵌入到containerView中,例如use this way its embed and load array will be showed

答案 1 :(得分:0)

尝试在animateResults的{​​{1}}中重新加载数据。像这样的东西,

completion block

答案 2 :(得分:0)

我终于来了!得到这个想法我的意图是在UIView内部加载UITableView。我试图在ViewController上拖放TableView委托和数据源,但每次加载应用程序时它都会崩溃。所以我在这里将表视图委托和数据源移动到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);

}];

}

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

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

}];
}

这个修剪过的代码有效,因为我将dataSource和委托连接到UIViewController。这样做会在UIViewController加载时加载表,因此不需要以编程方式执行此操作。此外,纠正错误标记的单元标识符消除了对registerClass变量的需要。希望这有助于。