2011-12-13 2 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' 속성으로 설정할 필요가 있습니까? 그리고 그것들을'release'합니까 ?? 'tableView : willDisplayCell : forRowAtIndexPath :'는 SIGABRT 에러를 내고 있습니다. – GAMA

+0

나를 위해 노력했습니다. – GAMA