简体   繁体   English

在TableView中重新加载数据

[英]reload data in tableview

I can't reload my data in my contactsTableView in iOS. 我无法在iOS的contactsTableView中重新加载数据。 I figured out that the cellForRowAtIndexPath function is not called. 我发现没有调用cellForRowAtIndexPath函数。

I call onGroupClick on screenController and reloadData get 4 objects but I can't see them on my contactsListView . 我呼吁onGroupClickscreenControllerreloadData获得4个对象,但我不能看到他们对我的contactsListView

screenController: 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:

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 . 您有一个UITableViewController You have added a method reloadData to your table view controller, and you call that. 您已将方法reloadData添加到表视图控制器中,然后调用该方法。 That is not the same thing as calling the tableView's reloadData method. 这与调用tableView的reloadData方法不同。

If you want that custom method to cause the table view to reload, it needs to call the table view's reloadData method: 如果希望该自定义方法导致重新加载表视图,则需要调用该表视图的reloadData方法:

- (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 当您执行此操作时,表视图将调用numberOfSectionsInTableView方法,然后调用numberOfRowsInSection (一次或多次),然后调用cellForRowAtIndexPath

check out your tableview datasource and delegate may solve it 检查您的tableview数据源,然后委托可以解决它

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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