Objective c UITableView:删除单元格

Objective c UITableView:删除单元格,objective-c,uitableview,uisearchdisplaycontroller,Objective C,Uitableview,Uisearchdisplaycontroller,伙计们,我有个小问题。我正在搜索我的UITableView。我搜索“iPhone”,它会向我返回一个结果。没关系。iPhone只有一个结果。但当我搜索iPad时,它有两个结果,它给了我两个结果,但第一行仍然显示iphone信息。第二个显示iPad的正确信息 当我再次点击搜索栏搜索另一个单词时,它会给我两行想要的ipad。但当我按下“取消”按钮时,它会返回到iPhone和iPad行 我做错了什么 谢谢 单元格和表格方法: - (NSInteger)tableView:(UITabl

伙计们,我有个小问题。我正在搜索我的UITableView。我搜索“iPhone”,它会向我返回一个结果。没关系。iPhone只有一个结果。但当我搜索iPad时,它有两个结果,它给了我两个结果,但第一行仍然显示iphone信息。第二个显示iPad的正确信息

当我再次点击搜索栏搜索另一个单词时,它会给我两行想要的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];

            }

谢谢大家。

您只需在代码的
部分设置标签,如果(cell==nil)
。只有在第一次创建单元格时才会调用它;在此之后,该单元将退出队列以供重用,并且您不会创建新单元。如果您希望像这样将单元格出列,则需要在If语句之外设置文本(以及可能更改的标签的任何其他属性)。为了获取标签的引用,您需要使用标记和
viewWithTag:


或者,如果你想简化事情,不要让你的表格单元格出列;虽然如果你有很多电池,这可能会导致性能问题。

是的,它起作用了。但当我搜索iphone时,他们是ipad。第一行被复制了。iphone一行不会消失。如何清除表格中的行?检查while循环中的listCod。。。查看调用while循环的次数是否正确。另外,当您分配并初始化它时,您应该使用self.listCod(带有自动释放),而不仅仅是listCod。您当前的实现看起来有一些内存泄漏。是的。它当前被正确调用。我不明白自动释放怎么办。我在这里设置了它,结果崩溃了。通过使用listCod而不是self.listCod,您正在创建一个新对象,并将listCod设置为指向该新对象。当您这样做时,listCod用来指向的对象仍保留在内存中,但没有访问它的指针。这是内存泄漏。但对于主要问题,请检查self.listCod在
numberofrowsin节:
中引用时包含的内容。它的元素数量是否正确?您看到的表格单元格是否比预期的多?当我打印self.listCod时,它会给出我想要的确切内容。事实上,当我研究的时候,我的争吵并没有结束。