将表视图值更新为详细信息标签部分时出现问题

时间:2011-01-24 06:48:06

标签: iphone tableview

我的应用程序是导航基础,我有两个Table ViewControllers,我从第二个tableview选择数据并更新到第一个tabaleview的详细信息标签。下面的代码我尝试但没有更新,我在“Updatetableviewlable”方法中获得secondtableview值,但我没有更新“cellForRowAtIndexPath”方法,我在做错误..请任何人指导我。

     @class First;

            @interface FertilityAppAppDelegate 


                First *firstObj;

            @property (nonatomic,retain) First *firstObj;

            @implementation FertilityAppAppDelegate
            @synthesize firstObj;


> didFinishLaunchingWithOptions


 //code 1
//  Updating detail view from second tableview to first tableview .


        firstObj= [[First alloc] initWithNibName:  @"First" bundle:nil];
        UINavigationController *aNavigationController = [[UINavigationController alloc]   initWithRootViewController: firstObj];
        self.navigationController = aNavigationController;
        [window addSubview:navigationController.view];
        [window makeKeyAndVisible];
        return YES;
 //code 2
// Not Updating detail view from second tableview to first tableview

      mainmenuObj = [[Mainmenu alloc] initWithNibName:@"Mainmenu" bundle:nil];
        firstObj= [[First alloc] initWithNibName:  @"First" bundle:nil];
        UINavigationController *aNavigationController = [[UINavigationController alloc]   initWithRootViewController: mainmenuObj];
        self.navigationController = aNavigationController;
        [window addSubview:navigationController.view];
        [window makeKeyAndVisible];
        return YES;         


        @interface First 
                 NSString *symptomlabel; FertilityAppAppDelegate *Appdelegate;
         @property (nonatomic,retain) NSString *symptomlabel    
        @implementation First
            @synthesize symptomlabel;


            - (void)viewDidLoad
            { 
                Appdelegate = (FertilityAppAppDelegate*)[[UIApplication sharedApplication] delegate];


            }

            -(void) Updatetableviewlable:(NSString*)lable
            {
                symptomlabel=[lable retain] ;
                NSLog(@"the update value is  %@", lable);


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

            UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil) 
            {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId] autorelease];
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;   
            }
            switch (indexPath.section) 
            {
                case 0:
                {   
                    cell.textLabel.text = [self.MenstruationtoWakingArray objectAtIndex:indexPath.row];

                    if(indexPath.row==0)
                    {
                        UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(160, 25, 125, 15)];
                        myLabel.text =symptomlabel;
                        myLabel.backgroundColor = [UIColor clearColor];
                        myLabel.textColor=[UIColor blueColor];
                        [self.tableView addSubview:myLabel];
                        NSLog(@"The Save file is:%@",symptomlabel);


                    }                    
                //statement case upto 10        
                }
        }           
        }
            @interface Second : UITableViewController 

            {


                FertilityAppAppDelegate *appDelegate;
            }


             @implementation Second tableview

                                viewDidLoad 

                                appDelegate = (FertilityAppAppDelegate
    *)[[UIApplication sharedApplication] delegate];


            - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
    *)indexPath
            {
             if(indexPath.section==0)
             {     
              if (indexPath.row==0)
              {       
                NSString *updatelable =@"None";
                [appDelegate.firstObj Updatetableviewlable:updatelable];
                 appDelegate.selectedRow =indexPath.row;
              }
              //statement upto row 5

             }
                [self.tableView reloadData]; 
            }

2 个答案:

答案 0 :(得分:2)

Updatetableviewlable:方法中,您要将lable分配给symptomlabel。在这种情况下,symptomlabel可能不会保留字符串lable。您尝试使用Updatetableviewlable:这样的方法。

- (void)Updatetableviewlable:(NSString*)lable {

    symptomlabel = [lable retain];
    NSLog(@"%@", symptomlabel);
}

并确保您的cellForRowAtIndexPath:方法看起来像这样。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ...

    UILabel *myLabel;

    if (cell == nil) {

        myLabel = [[UILabel alloc] initWithFrame:CGRectMake(160, 25, 125, 15)];
        myLabel.backgroundColor = [UIColor clearColor];
        myLabel.textColor=[UIColor blueColor];
        myLabel.text = symptomlabel;
        [myLabel setTag:1];
        [cell.contentView addSubview:myLabel];
        [myLabel release];

    } else {

        myLabel = [[cell contentView] viewWithTag:1];
        myLabel.text = symptomlabel;
    }

    ...
}

添加也请确保您使用viewWillAppear:的{​​{1}}方法重新加载表格视图。

First.m

答案 1 :(得分:0)

在表格视图上重新加载数据。这将再次调用您的cellForRowAtIndexPath。

在你的setter中添加[self.tableView reloadData];

编辑:

1。你必须发布你的标签。

2。其他问题,您可以在重复使用时为细胞添加许多标签。 Dest你是UITableViewCell的子类,并在那里进行自定义布局。这将改善iPhone 3G等慢速设备的滚动性能。尝试在你的cellForRowAtIndexPath中不要分配和初始化对象,也不要做计算代码(for(),while())。这将极大地改善您的滚动性能。

编辑2:

您必须在您的财产上设置(复制)或(保留)@property (nonatomic,retain) NSString *symptomlabel;