UITableView cellForRowAtIndexPath未被调用

ewi*_*ard 0 delegates datasource objective-c uitableview ios

我知道没有调用cellForRowAtIndexPath方法,因为我从来没有看到NSLog它内部的消息.我已经设置了dataSourcedelegate的属性UITableView,我也已经宣布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;
}
Run Code Online (Sandbox Code Playgroud)

Piy*_*rma 5

尝试做以下事情

1.)注册你的tableView

[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"SimpleTableItem"];
Run Code Online (Sandbox Code Playgroud)

2.)然后[_tableView reloadData]; 在下面添加

for (PFObject *object in objects) {
        NSLog(@"%@", object.objectId);
        tableData[countUsers] = [object valueForKey:@"username"];
        matchesForUser[countUsers] = [object valueForKey:@"matches"];
    }
Run Code Online (Sandbox Code Playgroud)

内部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]]);
}];
Run Code Online (Sandbox Code Playgroud)