滚动在搜索时替换表格视图单元格值

问题描述:

我正在执行具有自定义单元格的搜索条件。一切都很好,但在表格视图中搜索时,它会在第一行显示正确的单元格,但是当我滚动时,它会显示奇怪的行为。正在释放和重新分配的单元将替换已在其他行中的行中的值。我应该怎么做才能修正它们在相同索引处的单元格值。滚动在搜索时替换表格视图单元格值

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
NSInteger row = [indexPath row]; 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

contentForname = nil; 
contentFordesc = nil; 
contentForindicator = nil; 
contentForprice = nil; 
contentForchef = nil; 
contentForspecial = nil; 
contentForspicy = nil; 
contentForimage = nil; 

if (tableView == [[self searchDisplayController] searchResultsTableView]) 
{ 
    [self.tableview setHidden:YES]; 
    tableView.frame=CGRectMake(15, 92, 295, 361); 
    tableView.backgroundColor= [UIColor clearColor]; 


    contentForname  =[[self searchResults] objectAtIndex:0+j*8]; 
    contentFordesc  =[[self searchResults] objectAtIndex:1+j*8]; 
    contentForindicator =[[self searchResults] objectAtIndex:2+j*8]; 
    contentForprice  =[[self searchResults] objectAtIndex:3+j*8]; 
    contentForchef  =[[self searchResults] objectAtIndex:4+j*8]; 
    contentForspecial =[[self searchResults] objectAtIndex:5+j*8]; 
    contentForspicy  =[[self searchResults] objectAtIndex:6+j*8]; 
    contentForimage  =[[self searchResults] objectAtIndex:7+j*8]; 

    j++; 

} 
else 
{ 
contentForname  = [[[self contentsList] objectAtIndex:row]objectForKey:@"name"]; 
contentFordesc  = [[[self contentsList] objectAtIndex:row]objectForKey:@"description"]; 
contentForindicator = [[[self contentsList] objectAtIndex:row]objectForKey:@"indicator"]; 
contentForprice  = [[[self contentsList] objectAtIndex:row]objectForKey:@"price"];  
contentForchef  = [[[self contentsList] objectAtIndex:row]objectForKey:@"chef"]; 
contentForspecial = [[[self contentsList] objectAtIndex:row]objectForKey:@"special"]; 
contentForspicy  = [[[self contentsList] objectAtIndex:row]objectForKey:@"spicy"]; 
    contentForimage = [[[self contentsList] objectAtIndex:row]objectForKey:@"itemimage"]; 

    } 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    cell.selectionStyle = UITableViewCellSelectionStyleGray; 

    nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(120,20, 230, 20)]; 
    nameLabel.font = [UIFont fontWithName:@"Arial" size:16]; 
    nameLabel.textAlignment = UITextAlignmentLeft; 
    nameLabel.textColor = [UIColor blackColor]; 
    nameLabel.backgroundColor = [UIColor clearColor]; 
    nameLabel.tag = 100; 
    [cell addSubview:nameLabel]; 

    UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"edittext.png"]]; 
    [tempImageView setFrame:CGRectMake(5, 5, 297, 130)]; 
    [cell addSubview:tempImageView]; 
    [cell sendSubviewToBack:tempImageView]; 

    cellImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"veg.png"]]; 
    [cellImage setFrame:CGRectMake(10, 40, 50, 50)]; 
    [cell addSubview:cellImage]; 

    descTextview = [[UITextView alloc]initWithFrame:CGRectMake(120,50, 200, 100)]; 
    descTextview.backgroundColor = [UIColor clearColor]; 
    descTextview.tag=102; 
    descTextview.editable=FALSE; 
    descTextview.scrollEnabled=TRUE; 
    [cell addSubview:descTextview]; 

    priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(240, 15, 60, 25)]; 
    priceLabel.backgroundColor = [UIColor clearColor]; 
    priceLabel.font = [UIFont fontWithName:@"Arial" size:16]; 
    priceLabel.tag=101; 
    [cell addSubview:priceLabel]; 
} 
nameLabel = (UILabel*)[cell viewWithTag:100]; 
nameLabel.text = contentForname; 
priceLabel = (UILabel*)[cell viewWithTag:101]; 
priceLabel.text = [NSString stringWithFormat:@"Rs: %@",contentForprice]; 
descTextview = (UITextView*)[cell viewWithTag:102]; 
descTextview.text = contentFordesc; 
return cell; 
} 

实际上,当cellforrowatindexj创建的每个时间单元递增,这样我可以通过逻辑像0+j*8给索引正确的价值观从searchresult阵,但只要我滚动搜索时,在单元格值获得改变。总之,我无法修复搜索结果中的索引值。细胞价值似乎在洗牌。

+0

看起来像[cell viewWithTag:100];由于某种原因,返回零的重用单元格,你可以检查它吗?在开始滚动tableview之前,您可以在右侧打开断点。 – 2012-03-15 13:49:03

+0

什么是变量'j'?我在你的代码中没有看到你声明它的任何地方。我也对你的'searchResults'中的索引方案感到困惑;为什么它不同于'contentList'?另外,你在哪里声明'contentForname'和所有其他内容变量?他们是地方范围吗? (他们应该是)。 – FluffulousChimp 2012-03-15 14:11:12

+0

@alanduncan:j简直就是NSInteger。 contentForname和所有其他的NSMutablString在.h类。我不明白的问题'为什么它不同于contentList?' – Ketan 2012-03-17 08:30:19

对搜索结果表和主表中的单元使用不同的重用标识符,以使它们互不干扰。