2

皆さん、ここで少し問題があります。UITableView を検索しています。「iPhone」を検索すると、1 つの結果が返されます。大丈夫です。iPhone の結果は 1 つだけです。しかし、iPad を検索すると 2 つの結果が得られ、2 つの結果が得られますが、最初の行にはまだ iPhone 情報が表示されています。2 つ目は、iPad の正しい情報を表示します。

検索バーをもう一度クリックして別の単語を検索すると、目的の 2 行の ipad が表示されます。しかし、キャンセルボタンを押すと、iPhone と iPad の行に戻ります。

何が間違っているのですか?

ありがとう

セルとテーブルのメソッド:

        - (NSInteger)tableView:(UITableView *)tableView
         numberOfRowsInSection:(NSInteger)section {
            return [self.listCod count];
        }




        - (UITableViewCell *)tableView:(UITableView *)tableView
                 cellForRowAtIndexPath:(NSIndexPath *)indexPath {
            static NSString *MyIdentifier = @"SearchResult";
            UITableViewCell *cell = [tableView
                                     dequeueReusableCellWithIdentifier:MyIdentifier];

            if (cell == nil) {
                cell =
                [[[UITableViewCell alloc]
                  initWithFrame:CGRectMake(0, 0, 180, 200)
                  reuseIdentifier:MyIdentifier]
                 autorelease];


            UIColor *bkgColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"SearchViewTableCellBackground.png"]];

            UIView* backgroundView = [ [ [ UIView alloc ] initWithFrame:CGRectZero ] autorelease ];
            backgroundView.backgroundColor = bkgColor;
            cell.backgroundView = backgroundView;

            // Foto do produto

            UIView *teste = [[UIView alloc] init];
            teste.frame = CGRectMake(5, 5, 100, 100);
            teste.backgroundColor = [UIColor blueColor];
            [cell addSubview:teste];


            // Label de Marca

            UILabel *marca = [[UILabel alloc] init];
            marca.frame = CGRectMake(115, 10, 195, 25);
            marca.backgroundColor = [UIColor clearColor];
            marca.textColor = [UIColor grayColor];
            NSString *marcaString = [self.listMarca objectAtIndex:indexPath.row];
            marca.font = [UIFont systemFontOfSize:13.0];
            marca.text = marcaString;
            [cell addSubview:marca];
            [marca release];


            // Label do nome do produto

            UILabel *nome = [[UILabel alloc] init];
            nome.frame = CGRectMake(115, 30, 195, 25);
            nome.backgroundColor = [UIColor clearColor];
            nome.textColor = [[UIColor alloc] initWithRed:26.0 / 255 green:177.0 / 255 blue:240.0 / 255 alpha:1.0];

            nome.font = [UIFont boldSystemFontOfSize:25.0];
                NSString *noemString = [self.listName objectAtIndex:indexPath.row];
                nome.text = noemString;
            [cell addSubview:nome];
            [nome release];

            //Preco

            UILabel *preco = [[UILabel alloc] init];
            preco.frame = CGRectMake(115, 55, 195, 25);
            preco.backgroundColor = [UIColor clearColor];
            preco.textColor = [UIColor grayColor];


            // Manda preco e parcela pra tratar

            [self priceFormat:[self.listPreco objectAtIndex:indexPath.row] :[self.listParcela objectAtIndex:indexPath.row]];


            preco.font = [UIFont boldSystemFontOfSize:20.0];
            preco.text = self.precoFinal;
            [cell addSubview:preco];


            //Parcela

            UILabel *parcelaLabel = [[UILabel alloc] init];
            parcelaLabel.frame = CGRectMake(115, 73, 195, 25);
            parcelaLabel.backgroundColor = [UIColor clearColor];
            parcelaLabel.textColor = [UIColor grayColor];
            parcelaLabel.font = [UIFont systemFontOfSize:13.0];
            parcelaLabel.text = self.parcelamentoFinal;

            [cell addSubview:parcelaLabel];

            }



            return cell;
        }

検索バーのメソッド:

            - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
                [searchBar setShowsCancelButton:YES animated:YES];
                self.theTableView.allowsSelection = NO;
                self.theTableView.scrollEnabled = NO;
                [theTableView setRowHeight:110];
            }

            - (void)searchDisplayController:(UISearchDisplayController *)controller
             willShowSearchResultsTableView:(UITableView *)tableView
            {
                [tableView setRowHeight:[[self theTableView] rowHeight]];
                tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
                [self.tableData removeAllObjects];
                [self.theTableView reloadData];
            }

            - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
                searchBar.text=@"";

                [searchBar setShowsCancelButton:NO animated:YES];
                [searchBar resignFirstResponder];
                self.theTableView.allowsSelection = YES;
                self.theTableView.scrollEnabled = YES;
            }


            - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
                return NO;
            }

            - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {

                // Converte a String do campo de busca

                NSString* buscaGet =
                [searchBar.text stringByAddingPercentEscapesUsingEncoding:
                 NSASCIIStringEncoding];    


                // Conecta com a URL


                NSString *endereco = [[NSString alloc] initWithFormat:@"http://localhost/icomm/test.php?nome=%@", buscaGet];

                // Lê o resultado

                TBXML * tbxml = [[TBXML tbxmlWithURL:[NSURL URLWithString:endereco]] retain];
                TBXMLElement * rootXMLElement = tbxml.rootXMLElement;


                TBXMLElement * item = [TBXML childElementNamed:@"produto" parentElement:rootXMLElement];

                listCod = [[NSMutableArray alloc] init];

                listMarca = [[NSMutableArray alloc] init];

                listName = [[NSMutableArray alloc] init];

                listPreco = [[NSMutableArray alloc] init];

                listParcela = [[NSMutableArray alloc] init];

                while (item) {


                    // Seta a id do produto

                    TBXMLElement * codigo = [TBXML childElementNamed:@"item" parentElement:item];
                    NSString * codProd = [TBXML textForElement:codigo];

                    [self.listCod addObject:codProd];


                    // Seta a marca do produto

                    TBXMLElement * marca = [TBXML childElementNamed:@"marca" parentElement:item];
                    NSString * marcaProd = [TBXML textForElement:marca];    
                    [self.listMarca addObject:marcaProd];

                    // Seta o nome do produto

                    TBXMLElement * nome = [TBXML childElementNamed:@"nome" parentElement:item];
                    NSString * nomeProd = [TBXML textForElement:nome];
                    [self.listName addObject:nomeProd];


                    // Seta o preco do produto

                    TBXMLElement * preco = [TBXML childElementNamed:@"preco" parentElement:item];
                    NSString * precoProd = [TBXML textForElement:preco];    
                    [self.listPreco addObject:precoProd];



                    // Seta o parcela do produto

                    TBXMLElement * parc = [TBXML childElementNamed:@"parcela" parentElement:item];
                    NSString * parcProd = [TBXML textForElement:parc];
                    [self.listParcela addObject:parcProd];




                    // Procura o proximo produto.

                    item = [TBXML nextSiblingNamed:@"produto" searchFromElement:item
                            ];
                }


                NSLog(@"%@", self.listName);

                [searchBar setShowsCancelButton:NO animated:YES];
                [searchBar resignFirstResponder];
                self.theTableView.allowsSelection = YES;
                self.theTableView.scrollEnabled = YES;

                //Remove tudo da table e recarrega

                [[[self searchDisplayController] searchResultsTableView] performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];

                [tableData removeAllObjects];
                [theTableView reloadData];

            }

全てに感謝。

4

1 に答える 1

1

if (cell == nil)コードの一部内にラベルを設定しているだけです。これは、セルが最初に作成されたときにのみ呼び出されます。その後、セルは再利用のためにデキューされ、新しいセルは作成されません。そのようにセルをデキューする場合は、テキスト (および変更される可能性のあるラベルのその他のプロパティ) を if ステートメントの外に設定する必要があります。ラベルへの参照を取得するには、タグと を使用する必要がありますviewWithTag:

または、物事を単純化したい場合は、tableCells をデキューしないでください。ただし、多数のセルがある場合、これによりパフォーマンスの問題が発生する可能性があります。

于 2011-03-10T21:24:06.240 に答える