ios - UIView 内的 UITableView - 仅当在 uitableview 外滚动单元格时才加载数据

标签 ios objective-c uitableview

我在 UIViewController 中使用 UITableView 时遇到问题: 当我启动应用程序时,会显示 UITableView,但不会显示来自 _opponents 的数据。 当我开始滚动 UITableView 之外的第一个单元格时,现在这个单元格得到更新。 此外,当我手动触发 [_topponents reloaddata] 时,整个 UITableView 显示正确的数据。

我必须添加或更改什么,以便从头开始显示数据?

dataSourcedelegate 都与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/

相关文章:

ios - 基于短信的应用程序中 itunesconnect 中的演示帐户

objective-c - 如何避免我的 UITextView 被我的 UITableView 覆盖?

ios - 如何存储选定行的数组?

iPhone/Objective-C - 在字符串中搜索特定单词

ios - UItableview 行未在 swift4 中使用 Mapkit 显示

IOS 使用 CLLocationManager 从纬度/经度获取位置

iphone - 将typedef结构转换为NSMutableArray

ios - 从 NSMutableArray 创建字节数组

ios - 使用 Xcode 7 TableView 未在 ContainerView 中显示数据

ios - UITableview:如何禁用某些行而不是其他行的选择