Search code examples
iosuitableviewreloaddata

Reload data in tableView after method call


I have method which gets data from server to use in table problem is that when data is fetched if there are 3 items already in array and then becomes 5 after method call and we reload data then it makes duplicate of records

[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;
}

Solution

  • As while calling the methods (setUpData) you are getting same data twice or new data gets added in it.

    You need it to remove all old data and use new set of data, for this you need to remove/release all data from array and add new objects into it.

    in the method (if using ARC)

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