方法调用后重新加载tableView中的数据



我有一个从服务器获取数据用于表格的方法问题是,当获取数据时如果数组中已经有3项然后在方法调用后变为5项我们重新加载数据然后它会复制记录

[self saveData];
[self setUpData];
[tableView reloadData];

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];
    int count=[resultArray count];
    NSLog(@"resultArry Row Counts is %d",count);
    return [resultArray count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70.00;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CustomCellIdentifier = @"CustomCellIdentifier";
    CustomCellF *cell = (CustomCellF *)[tableView
                                        dequeueReusableCellWithIdentifier: CustomCellIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCellF"
                                                     owner:self options:nil];
        for(id oneObject in nib)
            if ([oneObject isKindOfClass:[CustomCellF class]])
                cell = (CustomCellF *)oneObject;
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];
    FeedbackData *theCellData = [resultArray objectAtIndex:indexPath.row];
    cell.theTitle.text =theCellData.user_Feedback;
    NSString*type=theCellData.user_Rating;
    if ([type isEqualToString:@"One Star"]) {
        cell.theCellImage1.image=[UIImage imageNamed:@"starblue.png"];
        cell.theCellImage2.image=[UIImage imageNamed:@"stargray.png"];
        cell.theCellImage3.image=[UIImage imageNamed:@"stargray.png"];
        cell.theCellImage4.image=[UIImage imageNamed:@"stargray.png"];
        cell.theCellImage5.image=[UIImage imageNamed:@"stargray.png"];
    }
    else if ([type isEqualToString:@"Two Stars"]) {
        cell.theCellImage1.image=[UIImage imageNamed:@"starblue.png"];
        cell.theCellImage2.image=[UIImage imageNamed:@"starblue.png"];
        cell.theCellImage3.image=[UIImage imageNamed:@"stargray.png"];
        cell.theCellImage4.image=[UIImage imageNamed:@"stargray.png"];
        cell.theCellImage5.image=[UIImage imageNamed:@"stargray.png"];
    }
    else if ([type isEqualToString:@"Three Stars"]) {
        cell.theCellImage1.image=[UIImage imageNamed:@"starblue.png"];
        cell.theCellImage2.image=[UIImage imageNamed:@"starblue.png"];
        cell.theCellImage3.image=[UIImage imageNamed:@"starblue.png"];
        cell.theCellImage4.image=[UIImage imageNamed:@"stargray.png"];
        cell.theCellImage5.image=[UIImage imageNamed:@"stargray.png"];
    }
    else if ([type isEqualToString:@"Four Stars"]) {
        cell.theCellImage1.image=[UIImage imageNamed:@"starblue.png"];
        cell.theCellImage2.image=[UIImage imageNamed:@"starblue.png"];
        cell.theCellImage3.image=[UIImage imageNamed:@"starblue.png"];
        cell.theCellImage4.image=[UIImage imageNamed:@"starblue.png"];
        cell.theCellImage5.image=[UIImage imageNamed:@"stargray.png"];
    }
    else {
        cell.theCellImage1.image=[UIImage imageNamed:@"starblue.png"];
        cell.theCellImage2.image=[UIImage imageNamed:@"starblue.png"];
        cell.theCellImage3.image=[UIImage imageNamed:@"starblue.png"];
        cell.theCellImage4.image=[UIImage imageNamed:@"starblue.png"];
        cell.theCellImage5.image=[UIImage imageNamed:@"starblue.png"];
    }
    return cell;
}

当调用方法(setUpData)时,您将获得相同的数据两次或新数据被添加在其中。

你需要它删除所有旧数据并使用新数据集,为此你需要从数组中删除/释放所有数据并向其中添加新对象。

方法中的

(如果使用ARC)

-(void) setUpData{
   if(yourArray!=nil){
         yourArray=nil;
         yourArray=[NSMutableArray new];
         //fill data again here
    }
} 

如果你想首先填充你的数组,让它们为nil,以避免你现在面临的问题。

if(dataArray!=nil){
dataArray=nil;
dataArray=[[NSMutableData alloc]init];
}

如果你想重新填充你的数组,那么首先你必须做空。

     if(resultArray.count >0)
     {
              [resultArray removeAllObjects];
      }

先释放数组:

[dataArray release];

或者如果你使用Arc,那么

[dataArray removeAllObjects];

然后检查并再次填充

if(dataArray!=nil){
    dataArray=nil;
    dataArray=[[NSMutableData alloc]init];
}

最新更新