简体   繁体   中英

UITableView cellForRowAtIndexPath not getting called

I know that the cellForRowAtIndexPath method isn't being called because I never see the NSLog messages inside of it. I have set the dataSource and delegate attributes of the UITableView and I also have declared UITableViewDelegate and UITableViewDataSource in the header file. What am I missing?

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

Try doing the following things

1.) Register your tableView like

[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"SimpleTableItem"];

2.) Then Add [_tableView reloadData]; just below

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

Inside if (!error) part..

So your Updated Code must look like

[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]]);
}];

1) Here you do need to use countUsers = 0 ;If you need than you need to increment the value inside for loop by 1.Otherwise it will overwrite the value at index 0 every time.

2) You are using background calling method to fetch data.So it will take time to fetch data and control will go further and execute following code.But At that time matchesForUser array is empty, and dictionary is also empty.So in cell it won't display anything and you will not see NSLOG from cell.

Instead of this

ini

dictionary = [NSDictionary dictionaryWithObjects:matchesForUser forKeys:tableData];
sortedFirstArray = [[dictionary allKeys] sortedArrayUsingSelector:@selector(compare:)];
sortedSecondArray = [dictionary objectsForKeys:sortedFirstArray notFoundMarker:[NSNull null]];

3) Try with this,

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]]);
    });
}];

Did you reload the Tableview in ViewDidLoad? like

[_tableView reloadData];

Reload the table again after

dictionary = [NSDictionary dictionaryWithObjects:matchesForUser forKeys:tableData];
sortedFirstArray = [[dictionary allKeys] sortedArrayUsingSelector:@selector(compare:)];
sortedSecondArray = [dictionary objectsForKeys:sortedFirstArray notFoundMarker:[NSNull null]];

[_tableView reloadData];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [tableData count];
}

The count is 0? you can type NSLog("%@", @(tableData.count)); or just return 1; for test. UITableView send the tableView:numberOfRowsInSection: first, if the method return 0. it will not call the cellForRowAtIndexPath .

And as @PiyushSharma mentioned, you should Register your tableView .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM