我在 UIViewController
中使用 UITableView
时遇到问题:
当我启动应用程序时,会显示 UITableView
,但不会显示来自 _opponents 的数据。
当我开始滚动 UITableView
之外的第一个单元格时,现在这个单元格得到更新。
此外,当我手动触发 [_topponents reloaddata] 时,整个 UITableView
显示正确的数据。
我必须添加或更改什么,以便从头开始显示数据?
dataSource
和delegate
都与storyboard 中的类关联
这是我的代码:
查看加载:
- (void)viewDidLoad {
[super viewDidLoad];
myappdel = (AppDelegate *)[UIApplication sharedApplication].delegate;
self.title = @"New Match";
_lopponent.text = _opponent;
opponents = [[NSMutableArray alloc] init];
NSMutableDictionary *opponent = [[NSMutableDictionary alloc] init];
[opponent setObject:@"opponent A" forKey:@"name"];
[opponent setObject:@"xx matches | xx wins || xx losts" forKey:@"stats"];
[opponent setObject:@"Krems" forKey:@"location"];
NSMutableDictionary *opponent2 = [[NSMutableDictionary alloc] init];
[opponent2 setObject:@"opponent B" forKey:@"name"];
[opponent2 setObject:@"yy matches | yy wins || yy losts" forKey:@"stats"];
[opponent2 setObject:@"Location B" forKey:@"location"];
NSMutableDictionary *opponent3 = [[NSMutableDictionary alloc] init];
[opponent3 setObject:@"opponent C" forKey:@"name"];
[opponent3 setObject:@"zz matches | zz wins || zz losts" forKey:@"stats"];
[opponent3 setObject:@"Location C" forKey:@"location"];
[opponents addObject:opponent];
[opponents addObject:opponent2];
[opponents addObject:opponent3];
[_topponents reloadData];
}
这里是我的 cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"opponentcell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *lopponentname = (UILabel*)[cell viewWithTag:1];
UILabel *lopponentstats = (UILabel*)[cell viewWithTag:2];
UILabel *llocation = (UILabel*)[cell viewWithTag:3];
lopponentname.text = [[opponents objectAtIndex:indexPath.row] objectForKey:@"name"];
lopponentstats.text = [[opponents objectAtIndex:indexPath.row] objectForKey:@"stats"];
llocation.text = [[opponents objectAtIndex:indexPath.row] objectForKey:@"location"];
return cell;
}
最佳答案
我发现了我的错!
我曾经写过:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]
但我应该使用
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
使用“forIndexPath”
关于ios - UIView 内的 UITableView - 仅当在 uitableview 外滚动单元格时才加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27184817/