2011-12-13 54 views
0

我正在填充表中包含每個行上的4個標籤,並在動態按鈕上單擊。UITableView數據沒有被刷新並且無法重新傳輸新數據

當我試圖動態地將不同的數據插入到同一個表中時,它不會刷新舊數據,因此舊數據會重疊在新數據上。

我需要做些什麼改變?

* .M

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    { 
    return 1; 
    } 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if(tableView==myTableView){ 

     return [tableData count]; 
    } 
    else if(tableView==aTableView){ 
     return [a count]; 
    } 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if(tableView==myTableView){ 
    static NSString *MyIdentifier = @"MyIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 
    } 
    cell.textLabel.font=[UIFont fontWithName:@"Arial" size:12.0]; 
    cell.textLabel.text = [tableData objectAtIndex:indexPath.row]; 
    cell.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
    return cell; 
} 
    else if(tableView==aTableView){ 

     static NSString *[email protected]"CellIdentifier"; 

     static NSInteger StateTag = 1; 
     static NSInteger CapitalTag = 2; 
     static NSInteger StateTag1 = 3; 
     static NSInteger StateTag2 = 4; 


     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if(cell == nil){ 

       cell=[[[UITableViewCell alloc]initWithFrame:CGRectZero reuseIdentifier:CellIdentifier]autorelease]; 
       CGRect frame; 
       frame.origin.x = 10; 
       frame.origin.y = 5; 
       frame.size.height = 35; 
       frame.size.width = 80; 


       UILabel *capitalLabel = [[UILabel alloc] initWithFrame:frame]; 
       capitalLabel.tag = CapitalTag; 
       [cell.contentView addSubview:capitalLabel]; 

       frame.origin.x += 135; 
       UILabel *stateLabel = [[UILabel alloc] initWithFrame:frame]; 
       stateLabel.tag = StateTag; 
       [cell.contentView addSubview:stateLabel]; 

       frame.origin.x += 100; 
       UILabel *stateLabel1 = [[UILabel alloc] initWithFrame:frame]; 
       stateLabel1.tag = StateTag1; 
       [cell.contentView addSubview:stateLabel1]; 

       frame.origin.x += 100; 
       UILabel *stateLabel2 = [[UILabel alloc] initWithFrame:frame]; 
       stateLabel2.tag = StateTag2; 
       [cell.contentView addSubview:stateLabel2]; 

       capitalLabel = (UILabel *) [cell.contentView viewWithTag:CapitalTag]; 
       stateLabel = (UILabel *) [cell.contentView viewWithTag:StateTag]; 
       stateLabel1 = (UILabel *) [cell.contentView viewWithTag:StateTag1]; 
       stateLabel2 = (UILabel *) [cell.contentView viewWithTag:StateTag2]; 

       capitalLabel.text=[a objectAtIndex:indexPath.row]; 
       stateLabel.text = [b objectAtIndex:indexPath.row]; 
       stateLabel1.text = [c objectAtIndex:indexPath.row]; 
       stateLabel2.text = [d objectAtIndex:indexPath.row]; 
      }    
      return cell; 
     } 
} 


-(IBAction)btnClicked:(id)sender{ 
    NSMutableDictionary *cells = (NSMutableDictionary*)[self.aTableView valueForKey:@"_reusableTableCells"]; 
    [cells removeAllObjects]; 

    [self gainer]; 
} 

-(IBAction)btnClicked1:(id)sender { 

    [self looser]; 
} 

-(void)gainer{ 

    arr1=[[NSMutableArray alloc]init]; 
    arr2=[[NSMutableArray alloc]init]; 
    a=[[NSMutableArray alloc]init]; 
    b=[[NSMutableArray alloc]init]; 
    c=[[NSMutableArray alloc]init]; 
    d=[[NSMutableArray alloc]init]; 

    [a removeAllObjects]; 
    [b removeAllObjects]; 
    [c removeAllObjects]; 
    [d removeAllObjects]; 

    //POPULATING OF ARRAYS(a,b,c,d) IS DONE HERE. NOW using ReloadData to populate table. 


    [self.aTableView reloadData]; 
} 

-(void)looser{ 
    //SIMILAR LOGIC AS DEFINED IN gainer() method 
} 



- (void)viewDidLoad { 

    myTableView = [[UITableView alloc]initWithFrame:CGRectMake(200, 31, 310,170)]; 
    myTableView.delegate = self; 
    myTableView.dataSource = self; 
    [self.view addSubview:myTableView]; 

    //code for fetch 
    arr1=[[NSMutableArray alloc]init]; 
    arr2=[[NSMutableArray alloc]init]; 
    a=[[NSMutableArray alloc]init]; 
    b=[[NSMutableArray alloc]init]; 
    c=[[NSMutableArray alloc]init]; 
    d=[[NSMutableArray alloc]init]; 

    aTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen]applicationFrame] style:UITableViewStyleGrouped]; 
    aTableView.dataSource = self; 
    aTableView.delegate = self; 
    aTableView.frame = CGRectMake(100, 150, 200, 300); 
    aTableView.tag=1; 
    [self.view addSubview:aTableView]; 

    [super viewDidLoad]; 
} 

有什麼建議?

回答

1

使用XIB創建自定義單元格。這會簡化你的代碼,實際上可能會加速你的代碼,因爲你沒有做標籤查找。爲需要設置的標籤創建直接屬性。如果標籤需要空白,則將nil傳遞給標籤的text屬性。

另一個提示是不要做子視圖定位tableview:cellForRowAtIndexPath:這可能會有不可預知的結果,而應改爲使用tableView:willDisplayCell:forRowAtIndexPath:

順便說一句,你有幾個泄漏與你分配但你沒有釋放的所有標籤。

+0

我需要將'nil'設置爲'label'的'text'屬性,我在哪裏釋放它們? 'tableView:willDisplayCell:forRowAtIndexPath:'給出SIGABRT錯誤。 – GAMA

+0

爲我工作。 – GAMA