UITableView cellForRowAtIndexPath 没有被调用
UITableView cellForRowAtIndexPath not getting called
我知道没有调用 cellForRowAtIndexPath 方法,因为我从未在其中看到 NSLog
消息。我已经设置了 UITableView
的 dataSource
和 delegate
属性,并且还在头文件中声明了 UITableViewDelegate
和 UITableViewDataSource
。我错过了什么?
- (void)viewDidLoad {
[self.view setBackgroundColor:[UIColor grayColor]];
tableData = [[NSMutableArray alloc] init];
matchesForUser = [[NSMutableArray alloc] init];
sortedFirstArray = [[NSArray alloc] init];
sortedSecondArray = [[NSArray alloc] init];
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 20) style:UITableViewStylePlain];
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
countUsers = 0;
PFQuery *query = [PFQuery queryWithClassName:@"_User"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
for (PFObject *object in objects) {
NSLog(@"%@", object.objectId);
tableData[countUsers] = [object valueForKey:@"username"];
matchesForUser[countUsers] = [object valueForKey:@"matches"];
}
}else{
NSLog([error description]);
}
NSLog(@"***tabledata***");
NSLog([NSString stringWithFormat:@"%lu", (unsigned long)[tableData count]]);
NSLog(@"***matchesdata***");
NSLog([NSString stringWithFormat:@"%lu", (unsigned long)[matchesForUser count]]);
}];
dictionary = [NSDictionary dictionaryWithObjects:matchesForUser forKeys:tableData];
sortedFirstArray = [[dictionary allKeys] sortedArrayUsingSelector:@selector(compare:)];
sortedSecondArray = [dictionary objectsForKeys:sortedFirstArray notFoundMarker:[NSNull null]];
backToMap = [[UIButton alloc] initWithFrame:CGRectMake(80, 518, 160, 30)];
backToMap.backgroundColor = [UIColor colorWithRed:33.0f/255.0f green:156.0f/255.0f blue:41.0f/255.0f alpha:1.0f];
[backToMap addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
[backToMap setTitle:@"BACK TO MAP" forState:UIControlStateNormal];
[self.view addSubview:backToMap];
}
- (void)dismiss {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSString *username = [[sortedFirstArray objectAtIndex:indexPath.row] stringByAppendingString:@" "];
NSString *matchAmount = [sortedSecondArray objectAtIndex:indexPath.row];
NSLog(@"***username***");
NSLog(username);
NSLog(@"***matchamount***");
NSLog(matchAmount);
cell.textLabel.text = [username stringByAppendingString:matchAmount];
return cell;
}
您是否在 ViewDidLoad 中重新加载了 Tableview?喜欢
[_tableView reloadData];
尝试做以下事情
1.) 像
一样注册你的tableView
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"SimpleTableItem"];
2.) 然后在
下面添加 [_tableView reloadData];
for (PFObject *object in objects) {
NSLog(@"%@", object.objectId);
tableData[countUsers] = [object valueForKey:@"username"];
matchesForUser[countUsers] = [object valueForKey:@"matches"];
}
内部 if (!error)
部分..
所以你的更新代码必须看起来像
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
for (PFObject *object in objects) {
NSLog(@"%@", object.objectId);
tableData[countUsers] = [object valueForKey:@"username"];
matchesForUser[countUsers] = [object valueForKey:@"matches"];
}
[_tableView reloadData];
}else{
NSLog([error description]);
}
NSLog(@"***tabledata***");
NSLog([NSString stringWithFormat:@"%lu", (unsigned long)[tableData count]]);
NSLog(@"***matchesdata***");
NSLog([NSString stringWithFormat:@"%lu", (unsigned long)[matchesForUser count]]);
}];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [tableData count];
}
count
是0?您可以键入 NSLog("%@", @(tableData.count));
或仅键入 return 1;
进行测试。
UITableView
先发送tableView:numberOfRowsInSection:
,如果方法return0.它不会调用cellForRowAtIndexPath
。
正如@PiyushSharma 提到的,你应该 Register your tableView
。
后重新加载 table
dictionary = [NSDictionary dictionaryWithObjects:matchesForUser forKeys:tableData];
sortedFirstArray = [[dictionary allKeys] sortedArrayUsingSelector:@selector(compare:)];
sortedSecondArray = [dictionary objectsForKeys:sortedFirstArray notFoundMarker:[NSNull null]];
[_tableView reloadData];
1) 在这里你确实需要使用 countUsers = 0;如果你需要比你需要增加 for 循环中的值 1.Otherwise 它会覆盖每次索引 0 处的值。
2) 您正在使用后台调用方法来获取 data.So 获取数据需要时间,控制将进一步执行 code.But 届时 matchesForUser 数组 为空,dictionary 也在单元格中 empty.So 它不会显示任何内容,您也不会从单元格中看到 NSLOG。
而不是这个
ini
dictionary = [NSDictionary dictionaryWithObjects:matchesForUser forKeys:tableData];
sortedFirstArray = [[dictionary allKeys] sortedArrayUsingSelector:@selector(compare:)];
sortedSecondArray = [dictionary objectsForKeys:sortedFirstArray notFoundMarker:[NSNull null]];
3) 试试这个,
PFQuery *query = [PFQuery queryWithClassName:@"_User"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (!error) {
for (PFObject *object in objects) {
NSLog(@"%@", object.objectId);
[tableData addObject:[object valueForKey:@"username"]];
[matchesForUser addObject:[object valueForKey:@"matches"]];
}
dictionary = [NSDictionary dictionaryWithObjects:matchesForUser forKeys:tableData];
sortedFirstArray = [[dictionary allKeys] sortedArrayUsingSelector:@selector(compare:)];
sortedSecondArray = [dictionary objectsForKeys:sortedFirstArray notFoundMarker:[NSNull null]];
[_tableView reloadData];
}else{
NSLog([error description]);
}
NSLog(@"***tabledata***");
NSLog([NSString stringWithFormat:@"%lu", (unsigned long)[tableData count]]);
NSLog(@"***matchesdata***");
NSLog([NSString stringWithFormat:@"%lu", (unsigned long)[matchesForUser count]]);
});
}];
我知道没有调用 cellForRowAtIndexPath 方法,因为我从未在其中看到 NSLog
消息。我已经设置了 UITableView
的 dataSource
和 delegate
属性,并且还在头文件中声明了 UITableViewDelegate
和 UITableViewDataSource
。我错过了什么?
- (void)viewDidLoad {
[self.view setBackgroundColor:[UIColor grayColor]];
tableData = [[NSMutableArray alloc] init];
matchesForUser = [[NSMutableArray alloc] init];
sortedFirstArray = [[NSArray alloc] init];
sortedSecondArray = [[NSArray alloc] init];
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 20) style:UITableViewStylePlain];
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
countUsers = 0;
PFQuery *query = [PFQuery queryWithClassName:@"_User"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
for (PFObject *object in objects) {
NSLog(@"%@", object.objectId);
tableData[countUsers] = [object valueForKey:@"username"];
matchesForUser[countUsers] = [object valueForKey:@"matches"];
}
}else{
NSLog([error description]);
}
NSLog(@"***tabledata***");
NSLog([NSString stringWithFormat:@"%lu", (unsigned long)[tableData count]]);
NSLog(@"***matchesdata***");
NSLog([NSString stringWithFormat:@"%lu", (unsigned long)[matchesForUser count]]);
}];
dictionary = [NSDictionary dictionaryWithObjects:matchesForUser forKeys:tableData];
sortedFirstArray = [[dictionary allKeys] sortedArrayUsingSelector:@selector(compare:)];
sortedSecondArray = [dictionary objectsForKeys:sortedFirstArray notFoundMarker:[NSNull null]];
backToMap = [[UIButton alloc] initWithFrame:CGRectMake(80, 518, 160, 30)];
backToMap.backgroundColor = [UIColor colorWithRed:33.0f/255.0f green:156.0f/255.0f blue:41.0f/255.0f alpha:1.0f];
[backToMap addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
[backToMap setTitle:@"BACK TO MAP" forState:UIControlStateNormal];
[self.view addSubview:backToMap];
}
- (void)dismiss {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSString *username = [[sortedFirstArray objectAtIndex:indexPath.row] stringByAppendingString:@" "];
NSString *matchAmount = [sortedSecondArray objectAtIndex:indexPath.row];
NSLog(@"***username***");
NSLog(username);
NSLog(@"***matchamount***");
NSLog(matchAmount);
cell.textLabel.text = [username stringByAppendingString:matchAmount];
return cell;
}
您是否在 ViewDidLoad 中重新加载了 Tableview?喜欢
[_tableView reloadData];
尝试做以下事情
1.) 像
一样注册你的tableView[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"SimpleTableItem"];
2.) 然后在
下面添加[_tableView reloadData];
for (PFObject *object in objects) {
NSLog(@"%@", object.objectId);
tableData[countUsers] = [object valueForKey:@"username"];
matchesForUser[countUsers] = [object valueForKey:@"matches"];
}
内部 if (!error)
部分..
所以你的更新代码必须看起来像
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
for (PFObject *object in objects) {
NSLog(@"%@", object.objectId);
tableData[countUsers] = [object valueForKey:@"username"];
matchesForUser[countUsers] = [object valueForKey:@"matches"];
}
[_tableView reloadData];
}else{
NSLog([error description]);
}
NSLog(@"***tabledata***");
NSLog([NSString stringWithFormat:@"%lu", (unsigned long)[tableData count]]);
NSLog(@"***matchesdata***");
NSLog([NSString stringWithFormat:@"%lu", (unsigned long)[matchesForUser count]]);
}];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [tableData count];
}
count
是0?您可以键入 NSLog("%@", @(tableData.count));
或仅键入 return 1;
进行测试。
UITableView
先发送tableView:numberOfRowsInSection:
,如果方法return0.它不会调用cellForRowAtIndexPath
。
正如@PiyushSharma 提到的,你应该 Register your tableView
。
dictionary = [NSDictionary dictionaryWithObjects:matchesForUser forKeys:tableData];
sortedFirstArray = [[dictionary allKeys] sortedArrayUsingSelector:@selector(compare:)];
sortedSecondArray = [dictionary objectsForKeys:sortedFirstArray notFoundMarker:[NSNull null]];
[_tableView reloadData];
1) 在这里你确实需要使用 countUsers = 0;如果你需要比你需要增加 for 循环中的值 1.Otherwise 它会覆盖每次索引 0 处的值。
2) 您正在使用后台调用方法来获取 data.So 获取数据需要时间,控制将进一步执行 code.But 届时 matchesForUser 数组 为空,dictionary 也在单元格中 empty.So 它不会显示任何内容,您也不会从单元格中看到 NSLOG。
而不是这个
ini
dictionary = [NSDictionary dictionaryWithObjects:matchesForUser forKeys:tableData];
sortedFirstArray = [[dictionary allKeys] sortedArrayUsingSelector:@selector(compare:)];
sortedSecondArray = [dictionary objectsForKeys:sortedFirstArray notFoundMarker:[NSNull null]];
3) 试试这个,
PFQuery *query = [PFQuery queryWithClassName:@"_User"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (!error) {
for (PFObject *object in objects) {
NSLog(@"%@", object.objectId);
[tableData addObject:[object valueForKey:@"username"]];
[matchesForUser addObject:[object valueForKey:@"matches"]];
}
dictionary = [NSDictionary dictionaryWithObjects:matchesForUser forKeys:tableData];
sortedFirstArray = [[dictionary allKeys] sortedArrayUsingSelector:@selector(compare:)];
sortedSecondArray = [dictionary objectsForKeys:sortedFirstArray notFoundMarker:[NSNull null]];
[_tableView reloadData];
}else{
NSLog([error description]);
}
NSLog(@"***tabledata***");
NSLog([NSString stringWithFormat:@"%lu", (unsigned long)[tableData count]]);
NSLog(@"***matchesdata***");
NSLog([NSString stringWithFormat:@"%lu", (unsigned long)[matchesForUser count]]);
});
}];