简体   繁体   中英

reload data in tableview

I can't reload my data in my contactsTableView in iOS. I figured out that the cellForRowAtIndexPath function is not called.

I call onGroupClick on screenController and reloadData get 4 objects but I can't see them on my contactsListView .

screenController:

-(void)onGroupClick:(NSString *)groupClickedId {
    NSLog(@"Group id: %@ was clicked on group tab", groupClickedId);
    contactsTableViewController* ctVC = (contactsTableViewController*)[childControllers objectAtIndex:1];
    ctVC.groupSelectedId = groupClickedId;
    [ctVC reloadData];
    [self switchTabs:1 ];
}

contactsTableViewController:

contactsTableViewController:

#import "contactsTableViewController.h"
#import "contactsTableViewCell.h"
#import "ModelUser.h"
#import "ModelGroup.h"
#import "userDetailsProfile.h"

@interface contactsTableViewController (){
    NSArray* usersId;
    NSMutableArray* usersData;
}

@end

@implementation contactsTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.groupSelectedId = @"";
    [self reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)reloadData {


    if([self.groupSelectedId isEqualToString:@""])
        NSLog(@"Contacts list is empty on first app run");

    else {

        NSLog(@"Contacts list for group id: %@ was loaded on contacts tab", self.groupSelectedId);

        //get id of my contacts in selected group
        usersId = [[ModelGroup instance] getGroup:self.groupSelectedId].contactsIdList;

        //get data of my contacts in selected group
        usersData = [[NSMutableArray alloc] init];
        for (int i=0; i< [usersId count] ; i++) {
            User* us = [[ModelUser instance] getUser:([usersId objectAtIndex:i])];
            [usersData addObject:us];
        }
    }
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return usersData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    contactsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"contactCell" forIndexPath:indexPath];

    User *us = [usersData objectAtIndex:indexPath.row];

    cell.contactsUserId = us.userId;
    cell.contactsName.text = [NSString stringWithFormat:@"%@ %@",us.fname,us.lname];
    [cell.contactsImage setFrame:CGRectMake(0, 0, 10, 10)];
    [cell.contactsImage setImage: [UIImage imageNamed:us.imageName]];
    [cell contactsSave:cell.contactsSave];
    [cell contactsAddToFav:cell.contactsAddToFav];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    User *us = [usersData objectAtIndex:indexPath.row];

    UIStoryboard* sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    userDetailsProfile*  udVC = [sb
                                 instantiateViewControllerWithIdentifier:@"userDetailsProfile"];
    udVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    udVC.userDetailId = [NSString stringWithFormat:@"%@", us.userId];
    [self showViewController:udVC sender:self];

}

/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];

    // Configure the cell...

    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)contactsAddToFav:(id)sender {
}

- (IBAction)contactsSave:(id)sender {
}
@end

You have a UITableViewController . You have added a method reloadData to your table view controller, and you call that. That is not the same thing as calling the tableView's reloadData method.

If you want that custom method to cause the table view to reload, it needs to call the table view's reloadData method:

- (void)reloadData {
    if([self.groupSelectedId isEqualToString:@""])
        NSLog(@"Contacts list is empty on first app run");
    else {
        NSLog(@"Contacts list for group id: %@ was loaded on contacts tab", 
          self.groupSelectedId);
        //get id of my contacts in selected group
        usersId = 
          [[ModelGroup instance] 
          getGroup:self.groupSelectedId].contactsIdList;

        //get data of my contacts in selected group
        usersData = [[NSMutableArray alloc] init];
        for (int i=0; i< [usersId count] ; i++) {
            User* us = [[ModelUser instance] getUser:([usersId objectAtIndex:i])];
            [usersData addObject:us];
        }
    }
    [self.tableView reloadData]; //Add this line.
}

When you do that the table view will call the numberOfSectionsInTableView method, then numberOfRowsInSection (once or more) and then cellForRowAtIndexPath

check out your tableview datasource and delegate may solve it

@interface contactsTableViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSArray* usersId;
    NSMutableArray* usersData;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.groupSelectedId = @"";
    [self reloadData];
}

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