在tableview中重新加载数据

时间:2015-12-31 02:17:48

标签: ios uitableview

我无法在iOS中的contactsTableView重新加载我的数据。我发现没有调用cellForRowAtIndexPath函数。

我在onGroupClick上致电screenControllerreloadData获取了4个对象,但我无法在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

2 个答案:

答案 0 :(得分:1)

你有UITableViewController。您已将方法reloadData添加到表视图控制器,然后调用它。这与调用tableView的reloadData方法不同。

如果您希望该自定义方法导致表视图重新加载,则需要调用表视图的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.
}

当您这样做时,表格视图会调用numberOfSectionsInTableView方法,然后numberOfRowsInSection(一次或多次),然后cellForRowAtIndexPath

答案 1 :(得分:0)

查看你的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];
}