UITableView reloadData在另一个类中不起作用

时间:2012-08-23 09:17:05

标签: ios xcode uitableview reloaddata

我在调用reloadData时遇到问题。我有两个课程,我无法更新我的桌子。

我的rootViewController.m

[self presentModalViewController:firstV animated:NO];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyCellIdentifier = @"MyCellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyCellIdentifier];
    if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyCellIdentifier];
         cell.selectionStyle = UITableViewCellSelectionStyleGray;
        //cell.textLabel.text = @"dupa";
        //cell.textLabel.font = [ UIFont fontWithName: @"Arial" size: 50.0 ];
            }

    NSLog(@"xxx ===== %@",firstVC.xxx);

    UIImageView *imgView = [[UIImageView alloc]init];
    UILabel *rankLabel = [[UILabel alloc]init];
    UILabel *rankDetail = [[UILabel alloc]init];
    UILabel *cyclesLabel = [[UILabel alloc]init];
    UILabel *cyclesDetail = [[UILabel alloc]init];
    UILabel *approxLabel = [[UILabel alloc]init];
    UILabel *approxDetail = [[UILabel alloc]init];
    UILabel *leftP = [[UILabel alloc]init];
    UILabel *rightP = [[UILabel alloc]init];
    UILabel *leftQ = [[UILabel alloc]init];
    UILabel *rightQ = [[UILabel alloc]init];
    UILabel *timeLabel = [[UILabel alloc]init];
    UILabel *timeDetail = [[UILabel alloc]init];
    UILabel *freeProductLabel = [[UILabel alloc]init];
    UILabel *freeDetailLabel = [[UILabel alloc]init];

    NSString *approxString;
    NSString *leftPeqsString;
    NSString *rightPeqsString;
    NSString *leftQString;
    NSString *rightQString;


    switch (indexPath.row) {
        case 0:
            imgView.frame = CGRectMake(10.0, 15.0, 80.0, 80.0);
            imgView.image = [UIImage imageNamed:@"Ambassador@2x.jpeg"];
            imgView.layer.cornerRadius = 40;
            imgView.layer.masksToBounds = YES;
            //imgView.contentMode = UIViewContentModeScaleAspectFit;
            //cell.imageView.image = imgView.image;
            [cell.contentView addSubview:imgView];
            rankLabel.frame = CGRectMake(110.0, 45.0, 200.0, 50.0);
            rankLabel.text = firstVC.xxx;
            rankLabel.textColor = [UIColor orangeColor];
            rankLabel.font = [UIFont boldSystemFontOfSize:28];
            [cell.contentView addSubview:rankLabel];
            rankDetail.text = @"Your current rank:";
            rankDetail.frame = CGRectMake(110.0, 30.0, 200.0, 30.0);
            rankDetail.font = [UIFont boldSystemFontOfSize:16];
            [cell.contentView addSubview:rankDetail];
            //cell.textLabel.font = [UIFont systemFontOfSize:20];
            //cell.textLabel.textColor = [UIColor orangeColor];
            break;...`

和:myFirstViewController.m

- (void)date:(NSString*)data
{
    ViewController *viewC = [[ViewController alloc]init];

    xxx = rank;
    NSLog(@"FVC rank xxx ==== %@", xxx);
    [viewC.tableView reloadData];

}

为什么这不允许我更新我的桌子?

2 个答案:

答案 0 :(得分:0)

reloadData没有被调用,因为viewC的视图没有被[[ViewController alloc]init];简单地加载到内存中因此,tableView没有被初始化,因此reloadData没有被调用tableView。

一旦您的viewC视图至少在屏幕上显示一次,您就需要调用此行。

答案 1 :(得分:0)

您需要在rootcolntrollerview.h(如下所示)中为tableview创建一个属性,并在rootcolntrollerview.m中进行综合(如果您通过IBOutlet创建tableview,则可以使用xib 。否则你可以删除它。)

@property (retain, nonatomic) IBOutlet UITableView *tableview_rootview;

然后您可以在myfirstviewController.m中重新加载数据,如下所示:

[viewC.tableview_rootview reloadData];