Values changing in tableview after performing search

68 views Asked by At

I'm getting correct data after loading the page as shown in this screenenter image description here

getting correct data even after performing search like this image enter image description here

but after search is cancelled im getting wrong data (label should be ph:+ and Fax:+ but its displaying both as Fax:+ like thisenter image description here

with this code

@implementation Staffdir
@synthesize tableview,filteredContentList,searchBar1;
- (void)viewDidLoad {
    searchBar1.delegate = self;
         __block   StaffDirectoryModel*staffdirectorymodel;
        NSURL *url = [NSURL URLWithString:@"http://dev.devobal.com/GetData.aspx?Query=select%20*%20from%20tb_RHP_Staff_Directory"];
    _staffdirarr=[[NSMutableArray alloc]init];
    NSMutableURLRequest *req=[NSMutableURLRequest requestWithURL:url];

    [[[NSURLSession sharedSession]dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSMutableArray *jsonarr=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

        dispatch_sync(dispatch_get_main_queue(), ^{
            // Update UI
            for(int i=0;i<jsonarr.count;i++){
                staffdirectorymodel=[[StaffDirectoryModel alloc]init];
                staffdirectorymodel.name =[[jsonarr objectAtIndex:i]objectForKey:@"Name"];
                staffdirectorymodel.occupation=[[jsonarr objectAtIndex:i]objectForKey:@"Job_Title"];

                staffdirectorymodel.phonelabel =[[jsonarr objectAtIndex:i]objectForKey:@"Phone"];

                staffdirectorymodel.faxlabel =[[jsonarr objectAtIndex:i]objectForKey:@"Fax_Number"];
                 staffdirectorymodel.department =[[jsonarr objectAtIndex:i]objectForKey:@"Department"];
                [_staffdirarr addObject:staffdirectorymodel];
                staffdirectorymodel=nil;
            }
                       [tableview reloadData];

        });




    }]resume];
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
//-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//    return 1;
//}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (isSearching) {
        return [searchResults count];
    }else{
        return _staffdirarr.count;
    }

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

    StaffDirectoryModel*staffdirectorymodel=[[StaffDirectoryModel alloc]init];
    Staffcustom *cell= [tableView dequeueReusableCellWithIdentifier:cellidentifer];

    if (cell == nil) {

        cell = [[Staffcustom alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifer];
    }
    [cell.fxlabel setHidden:NO];
    [cell.faxnumlabel setHidden:NO];
    if (isSearching) {

          staffdirectorymodel=[searchResults objectAtIndex:indexPath.row];


        cell.namelabel.text = staffdirectorymodel.name;
        cell.occupationlabel.text = staffdirectorymodel.occupation;

        NSString*tempph=staffdirectorymodel.phonelabel;
        NSString*tempfax=staffdirectorymodel.faxlabel;


        if(tempph.length!=0)
        {
            cell.phonelabel.text = staffdirectorymodel.phonelabel;
        }
        else{
            cell.phlabel.text=@"Fax: +";
            cell.phonelabel.text = staffdirectorymodel.faxlabel;
            [cell.fxlabel setHidden:YES];
            [cell.faxnumlabel setHidden:YES];

        }


        if(tempfax.length==0)
        {
            [cell.faxnumlabel setHidden:YES];
            [cell.fxlabel setHidden:YES];

        }

        if([tempph length]!=0 &&[tempfax length]!=0){
            cell.faxnumlabel.text = staffdirectorymodel.faxlabel;
        }



        cell.Depart.text = staffdirectorymodel.department;






    }
    else{

        staffdirectorymodel=[_staffdirarr objectAtIndex:indexPath.row];


        cell.namelabel.text = staffdirectorymodel.name;
    cell.occupationlabel.text = staffdirectorymodel.occupation;
        NSString*tempph=staffdirectorymodel.phonelabel;
        NSString*tempfax=staffdirectorymodel.faxlabel;
        if(tempph.length!=0)
        {
        cell.phonelabel.text = staffdirectorymodel.phonelabel;
        }
        else{
            cell.phlabel.text=@"Fax: +";
            cell.phonelabel.text = staffdirectorymodel.faxlabel;
            [cell.fxlabel setHidden:YES];
            [cell.faxnumlabel setHidden:YES];

        }

        if(tempfax.length==0)
        {
                [cell.faxnumlabel setHidden:YES];
                [cell.fxlabel setHidden:YES];

        }

        if([tempph length]!=0 &&[tempfax length]!=0){
    cell.faxnumlabel.text = staffdirectorymodel.faxlabel;
        }
    cell.Depart.text = staffdirectorymodel.department;

    }
     cell.namelabel.textColor = [UIColor blueColor];
    cell.occupationlabel.textColor=[UIColor colorWithRed:0.5 green:0.0 blue:1 alpha:1.0];

    UIView * additionalSeparator = [[UIView alloc] initWithFrame:CGRectMake(0,cell.frame.size.height-1,cell.frame.size.width,0.1)];
    additionalSeparator.backgroundColor = [UIColor blueColor];
    [cell addSubview:additionalSeparator];
        return cell;

}
//- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
//    isSearching = YES;
//}
-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)searchText
{
    //[filteredContentList removeAllObjects];

    if(searchText.length!=0){
        isSearching=YES;
        NSPredicate *resultPredicate = [NSPredicate
                                        predicateWithFormat:@"name contains[c] %@",
                                        searchText];

        searchResults=(NSMutableArray *)[_staffdirarr filteredArrayUsingPredicate:resultPredicate];
        [tableview reloadData];
    }
    else{
        isSearching=NO;
        [tableview reloadData];
    }

    //
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    isSearching=YES;
    [tableview reloadData];}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    isSearching=NO;
    [tableview reloadData];}

and staffdirectory model class

@interface StaffDirectoryModel : NSObject
@property (nonatomic,retain) NSString*name;
@property (nonatomic,retain) NSString*occupation;
@property (nonatomic,retain) NSString*phlabel;
@property (nonatomic,retain) NSString*phonelabel;
@property (nonatomic,retain) NSString*fxlabel;
@property (nonatomic,retain) NSString*faxlabel;
@property (nonatomic,retain) NSString*department;

staffcustom.h

@interface Staffcustom : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *fxlabel;

@property (weak, nonatomic) IBOutlet UILabel *namelabel;
@property (weak, nonatomic) IBOutlet UILabel *occupationlabel;
@property (weak, nonatomic) IBOutlet UILabel *phlabel;
@property (weak, nonatomic) IBOutlet UILabel *faxlabel;
@property (weak, nonatomic) IBOutlet UILabel *phonelabel;
@property (weak, nonatomic) IBOutlet UILabel *faxnumlabel;
@property (nonatomic,retain) IBOutlet UILabel *Depart;
@end
0

There are 0 answers