搜索栏未显示正常结果

时间:2016-12-16 07:32:54

标签: ios objective-c search uisearchbar

在我的自定义单元格中,有三个标签:名称,日期和时间。

当我从namearray搜索时,它会显示正确的过滤结果,但我的日期和时间标签值保持不变,它始终会显示我的第一个和第二个记录的日期和时间详细信息的值。

enter image description here

代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        // Return the number of rows in the section.

        if(isFiltered)
        {
            return [arrsearchresult count];
        }

        return [arrfullname count];
    }



    // tableView delegate methods
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            static NSString *simpleTableIdentifier = @"SimpleTableCell";

            AppoinmentTableViewCell *cell = (AppoinmentTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

            if (cell == nil)
            {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AppoinmentTableViewCell" owner:self options:nil];
                cell = [nib objectAtIndex:0];

                cell.btnCall.tag = indexPath.row;
                cell.btnMail.tag = indexPath.row;
                cell.btnSms.tag = indexPath.row;
                cell.btnCreatePlan.tag = indexPath.row;
                cell.btnAppointment.tag = indexPath.row;
                cell.btnStatus.tag=indexPath.row;

            }

            cell.clipsToBounds = YES;

            if ([strCheckVC isEqualToString:@"OpenLead"])
            {
                cell.lblFullname.text = [arrfullname objectAtIndex:indexPath.row];

                [self databaseOpen];

                NSString *query_user=[ NSString stringWithFormat:@"Select * from lead_status where row_lead_id = %@ and lead_followup_datetime != ' ' order by rowid desc limit 1",[arrRowid objectAtIndex:indexPath.row]];

                NSArray *rowids = [[NSArray alloc]init];
                rowids =[[database executeQuery:query_user]mutableCopy];

                NSLog(@"%@",rowids);

                [database close];

                if (rowids.count > 0 )
                {
                    NSString *dateString = [[rowids valueForKey:@"lead_followup_datetime"]objectAtIndex:0];

                    NSArray *components = [dateString componentsSeparatedByString:@" "];

                    NSString *date = components[0];
                    NSString *time = components[1];

                    // date convert
                    NSString *dateStr = [NSString stringWithFormat:@"%@",date];

                    NSString *timestr = [NSString stringWithFormat:@"%@",time];


                    // Convert string to date object
                    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
                    [dateFormat setDateFormat:@"HH:mm:ss"];
                    NSDate *date11 = [dateFormat dateFromString:timestr];



                    NSDateFormatter* df = [[NSDateFormatter alloc]init];
                    [df setDateFormat:@"hh:mm a"];
                    NSString *depResult = [df stringFromDate:date11];

                    cell.lblTime.text = depResult;

                    dateStr = [NSString stringWithFormat:@"%@",date];

                    // Convert string to date object
                    dateFormat = [[NSDateFormatter alloc] init];
                    [dateFormat setDateFormat:@"yyyy-mm-dd"];
                    NSDate *viewdt = [dateFormat dateFromString:dateStr];
                    //date11 = [dateFormat dateFromString:dateStr];
                    NSLog(@"%@",date);



                    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
                    NSDate *date22 = [dateFormatter dateFromString:dateStr];

                    [dateFormatter setDateFormat:@"dd MMM yyyy"];
                    NSString *dateWithNewFormat = [dateFormatter stringFromDate:date22];
                    NSLog(@"dateWithNewFormat: %@", dateWithNewFormat);


                    //            df = [[NSDateFormatter alloc]init];
                    //            [df setDateFormat:@"dd MMM yyyy"];
                    //            depResult = [df stringFromDate:viewdt];
                    //
                    cell.lblDate.text = dateWithNewFormat;



                }
                else
                {
                    // date convert
                    NSString *dateStr = [NSString stringWithFormat:@"%@",[appointmenttime objectAtIndex:indexPath.row]];

                    // Convert string to date object
                    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
                    [dateFormat setDateFormat:@"HH:mm:ss"];
                    NSDate *date = [dateFormat dateFromString:dateStr];
                    NSLog(@"%@",date);

                    NSDateFormatter* df = [[NSDateFormatter alloc]init];
                    [df setDateFormat:@"hh:mm a"];
                    NSString *depResult = [df stringFromDate:date];

                    cell.lblTime.text = depResult;


                    dateStr = [NSString stringWithFormat:@"%@",[arrappointment_date objectAtIndex:indexPath.row]];

                    // Convert string to date object
                    dateFormat = [[NSDateFormatter alloc] init];
                    [dateFormat setDateFormat:@"yyyy-mm-dd"];
                    date = [dateFormat dateFromString:dateStr];
                    NSLog(@"%@",date);




                    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
                    NSDate *date22 = [dateFormatter dateFromString:dateStr];

                    [dateFormatter setDateFormat:@"dd MMM yyyy"];
                    NSString *dateWithNewFormat = [dateFormatter stringFromDate:date22];
                    NSLog(@"dateWithNewFormat: %@", dateWithNewFormat);


                    //            df = [[NSDateFormatter alloc]init];
                    //            [df setDateFormat:@"dd MMM yyyy"];
                    //            depResult = [df stringFromDate:date];
                    //
                    cell.lblDate.text = dateWithNewFormat;



                }


                cell.selectionStyle = UITableViewCellSelectionStyleNone;


                if ([[arremail objectAtIndex:indexPath.row] isEqual:[NSNull null]] || [[arremail objectAtIndex:indexPath.row] isEqualToString:@"<null>"] || [[arremail objectAtIndex:indexPath.row] isEqualToString:@""] )
                {

                    cell.btnMail.hidden = NO;
                }


                NSUInteger hours_passed = [[arrhours_passed objectAtIndex:indexPath.row] integerValue];

                if (hours_passed <= 72)
                {

                    cell.colorView.backgroundColor = [UIColor whiteColor];

                }

                else if (hours_passed <= 144)
                {
                    cell.colorView.backgroundColor = [UIColor colorWithRed:219.0/255.0 green:157.0/255.0 blue:157.0/255.0 alpha:1.0];

                    cell.lblDate.textColor = [UIColor whiteColor];
                    cell.lblTime.textColor = [UIColor whiteColor];
                    cell.lblFullname.textColor = [UIColor whiteColor];


                }
                else if (hours_passed <= 216)
                {
                    cell.colorView.backgroundColor = [UIColor colorWithRed:202.0/255.0 green:114.0/255.0 blue:114.0/255.0 alpha:1.0];
                    cell.lblDate.textColor = [UIColor whiteColor];
                    cell.lblTime.textColor = [UIColor whiteColor];
                    cell.lblFullname.textColor = [UIColor whiteColor];
                }
                else if (hours_passed <= 288)
                {
                    cell.colorView.backgroundColor = [UIColor colorWithRed:189.0/255.0 green:78.0/255.0 blue:78.0/255.0 alpha:1.0];
                    cell.lblDate.textColor = [UIColor whiteColor];
                    cell.lblTime.textColor = [UIColor whiteColor];
                    cell.lblFullname.textColor = [UIColor whiteColor];
                }



                [self databaseOpen];


                NSString *str = [NSString stringWithFormat:@"Select * from lead_followup where rowid=%@",[arrRowid objectAtIndex:indexPath.row]];


                NSMutableArray *arr = [[NSMutableArray alloc]init];
                arr = [[database executeQuery:str]mutableCopy];
                NSLog(@"arr data %@",arr);


                [database close];

                NSString *strsms;




                if (arr.count != 0)
                {
                    strsms =  [NSString stringWithFormat:@"%@",[[arr valueForKey:@"followed_by_sms"]objectAtIndex:0]];

                    if ([strsms isEqualToString:@"0"])
                    {
                        if (hours_passed <= 72)
                        {
                            [cell.btnSms setImage:[UIImage imageNamed:@"Chatgray"] forState:UIControlStateNormal];

                        }
                        else if (hours_passed > 72)
                        {
                            //white

                            [cell.btnSms setImage:[UIImage imageNamed:@"chatwhite"] forState:UIControlStateNormal];

                        }
                    }
                    /*else
                     {
                     // green icon
                     [cell.btnSms setImage:[UIImage imageNamed:@"Chatgreen"] forState:UIControlStateNormal];
                     }*/


                    NSString *strCall =  [NSString stringWithFormat:@"%@",[[arr valueForKey:@"followed_by_call"]objectAtIndex:0]];
                    NSLog(@"%@",strCall);
                    if ([strCall isEqualToString:@"0"])
                    {
                        if (hours_passed <= 72)
                        {
                            //blackCallGray

                            [cell.btnCall setImage:[UIImage imageNamed:@"CallGray"] forState:UIControlStateNormal];


                        }
                        else if (hours_passed > 72)
                        {
                            [cell.btnCall setImage:[UIImage imageNamed:@"CallWhite"] forState:UIControlStateNormal];

                        }
                    }
                    else
                    {
                        [cell.btnCall setImage:[UIImage imageNamed:@"CallGreen"] forState:UIControlStateNormal];
                        // green icon

                    }



                    NSString *strmail =  [NSString stringWithFormat:@"%@",[[arr valueForKey:@"followed_by_mail"]objectAtIndex:0]];
                    NSLog(@"%@",strmail);

                    if ([strmail isEqualToString:@"0"])
                    {

                        if (hours_passed <= 72)
                        {

                            [cell.btnMail setImage:[UIImage imageNamed:@"Mailgray"] forState:UIControlStateNormal];

                        }
                        else if (hours_passed > 72)
                        {
                            [cell.btnMail setImage:[UIImage imageNamed:@"MailWhite"] forState:UIControlStateNormal];
                            //white

                        }
                    }
                    /*else
                     {
                     [cell.btnMail setImage:[UIImage imageNamed:@"MailGreen"] forState:UIControlStateNormal];
                     // green icon


                     }*/


                }


                [cell.btnCall addTarget:self
                                 action:@selector(callDo:) forControlEvents:UIControlEventTouchDown];

                [cell.btnSms addTarget:self
                                action:@selector(smsDo:) forControlEvents:UIControlEventTouchDown];
                [cell.btnMail addTarget:self
                                 action:@selector(mailDo:) forControlEvents:UIControlEventTouchDown];


                [cell.btnStatus addTarget:self
                                   action:@selector(Status:) forControlEvents:UIControlEventTouchDown];

                [cell.btnAppointment addTarget:self
                                        action:@selector(AddappointmentId:) forControlEvents:UIControlEventTouchDown];

                [cell.btnCreatePlan addTarget:self
                                       action:@selector(CreatePlan:) forControlEvents:UIControlEventTouchDown];
                if(self.btnSearchByName.selected==YES)
                {
                    if(isFiltered)
                    {
                        cell.lblFullname.text = [arrsearchresult objectAtIndex:indexPath.row];
                        /*cell.lblDate.text = [arrsearchresult objectAtIndex:indexPath.row];
                         cell.lblTime.text = [arrsearchresult objectAtIndex:indexPath.row];*/
                    }
                }
                else
                {
                    if(isFiltered)
                    {
                        //cell.lblFullname.text = [arrsearchresult objectAtIndex:indexPath.row];
                        cell.lblDate.text = [arrsearchresult objectAtIndex:indexPath.row];
                        //cell.lblTime.text = [arrsearchresult objectAtIndex:indexPath.row];
                    }
                }



            }
            else
            {

                cell.lblFullname.text = [arrfullname objectAtIndex:indexPath.row];
                cell.btnSms.hidden = YES;
                cell.btnCall.hidden = YES;
                cell.btnMail.hidden = YES;
                cell.colorView.hidden  =YES;



                //  cell.lblTime.text = [appointmenttime objectAtIndex:indexPath.row];
                //cell.lblDate.text = [arrappointment_date objectAtIndex:indexPath.row];



                NSString *dateStr = [NSString stringWithFormat:@"%@",[appointmenttime objectAtIndex:indexPath.row]];

                // Convert string to date object
                NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
                [dateFormat setDateFormat:@"HH:mm:ss"];
                NSDate *date = [dateFormat dateFromString:dateStr];
                NSLog(@"%@",date);

                NSDateFormatter* df = [[NSDateFormatter alloc]init];
                [df setDateFormat:@"hh:mm a"];
                NSString *depResult = [df stringFromDate:date];

                cell.lblTime.text = depResult;



                dateStr = [NSString stringWithFormat:@"%@",[arrappointment_date objectAtIndex:indexPath.row]];

                // Convert string to date object
                dateFormat = [[NSDateFormatter alloc] init];
                [dateFormat setDateFormat:@"yyyy-mm-dd"];
                date = [dateFormat dateFromString:dateStr];
                NSLog(@"%@",date);



                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                [dateFormatter setDateFormat:@"yyyy-MM-dd"];
                NSDate *date22 = [dateFormatter dateFromString:dateStr];

                [dateFormatter setDateFormat:@"dd MMM yyyy"];
                NSString *dateWithNewFormat = [dateFormatter stringFromDate:date22];
                NSLog(@"dateWithNewFormat: %@", dateWithNewFormat);

                //        df = [[NSDateFormatter alloc]init];
                //        [df setDateFormat:@"dd MMM yyyy"];
                //        depResult = [df stringFromDate:date];
                //
                cell.lblDate.text = dateWithNewFormat;


            }



            /*  UIView *bgColorView = [[UIView alloc] init];
             bgColorView.backgroundColor = [UIColor clearColor];
             [cell setSelectedBackgroundView:bgColorView];*/

             if(self.btnSearchByName.selected==YES)
             {
                 if(isFiltered)
                 {
                     cell.lblFullname.text = [arrsearchresult objectAtIndex:indexPath.row];
                     /*cell.lblDate.text = [arrsearchresult objectAtIndex:indexPath.row];
                      cell.lblTime.text = [arrsearchresult objectAtIndex:indexPath.row];*/
                 }
             }
            else
            {
                if(isFiltered)
                {
                     //cell.lblFullname.text = [arrsearchresult objectAtIndex:indexPath.row];
                        cell.lblDate.text = [arrsearchresult objectAtIndex:indexPath.row];
                     //cell.lblTime.text = [arrsearchresult objectAtIndex:indexPath.row];
                }
            }


            return cell;
        }






    - (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
        {
        if ([strCheckVC isEqualToString:@"OpenLead"])
        {
            if(self.btnSearchByName.selected==YES)
            {

                if(searchText.length==0)
                {
                    isFiltered=NO;
                }

                else
                {
                    isFiltered=YES;
                    arrsearchresult=[[NSMutableArray alloc]init];
                    for ( NSString * str in arrfullname)
                    {
                        NSRange stringRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch];
                        if(stringRange.location != NSNotFound)
                        {
                            [arrsearchresult addObject:str];
                        }
                    }
                }

            }


            else
            {
                if(searchText.length==0)
                {
                    isFiltered=NO;
                }

                else
                {
                    isFiltered=YES;
                    arrsearchresult=[[NSMutableArray alloc]init];
                    for ( NSString * str in arrleadCreatedDate)
                    {
                        NSRange stringRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch];
                        if(stringRange.location != NSNotFound)
                        {
                            [arrsearchresult addObject:str];
                        }
                    }
                }

            }
        }

        else
        {
            if(self.btnSearchByName.selected==YES)
            {

                if(searchText.length==0)
                {
                    isFiltered=NO;
                }

                else
                {
                    isFiltered=YES;
                    arrsearchresult=[[NSMutableArray alloc]init];
                    for ( NSString * str in arrfullname)
                    {
                        NSRange stringRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch];
                        if(stringRange.location != NSNotFound)
                        {
                            [arrsearchresult addObject:str];
                        }
                    }
                }

            }


            else
            {
                if(searchText.length==0)
                {
                    isFiltered=NO;
                }

                else
                {
                    isFiltered=YES;
                    arrsearchresult=[[NSMutableArray alloc]init];
                    NSString *searchString = searchBar.text;
                    for ( NSString * str in arrdatetime)
                    {
                        /*NSRange stringRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch];
                        if(stringRange.location != NSNotFound)
                        {
                            [arrsearchresult addObject:str];
                        }*/

                        NSComparisonResult result = [str compare:searchString options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchString length])];
                        if (result == NSOrderedSame) {
                            [arrsearchresult addObject:str];
                        }
                    }
                }

            }

        }




            [self.tblAppoinment reloadData];
        }

        -(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
            [self.tblAppoinment resignFirstResponder];
        }

0 个答案:

没有答案