我正在实施具有自定义单元格的搜索条件。一切都很顺利但是在表格视图中搜索它在第一行显示正确的单元格但是当我滚动时它显示奇怪的行为..解除分配和重新分配的单元格替换已经在其他行中的行中的值。我该怎么做才能修复它们在同一索引处的单元格值。
- (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;
}
实际上,每次在cellforrowatindex
j
创建单元格时都会递增,以便我可以通过像searchresult
这样的逻辑为0+j*8
数组的索引提供正确的值,但是只要我在搜索时滚动,单元格中的值就会发生变化。简而言之,我无法修复搜索结果中的索引值。似乎细胞价值洗牌。
答案 0 :(得分:0)
对搜索结果表和主表中的单元格使用不同的重用标识符,以便它们不会相互干扰。