【问题标题】:Table view cell images change after searching搜索后表格视图单元格图像发生变化
【发布时间】:2012-10-31 10:04:22
【问题描述】:

我正在处理表格视图单元格,其中包含附件视图上的不同图像和每个单元格左侧的数据(文本)。最初所有单元格都被其相关数据和图像占据。但是当我在 searchdisplaycontroller 上搜索时,图像是在新数组中分配。 例如;我有表格视图行,其中包含火车、公共汽车、飞机的图像以及相应的文本。但是当我搜索公共汽车时,文本看起来很好,但 imageview 显示不相关的图像(比如火车的图像)。 实现上述要求的逻辑可能是什么。我使用以下代码配置单元

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{
    btnImage = [[UIButton buttonWithType:UIButtonTypeCustom]retain];
    btnImage.frame = CGRectMake(688, 0, 80, 80);
    [btnImage setBackgroundImage:[arrImages objectAtIndex:indexPath.row] forState:UIControlStateNormal];
    [btnImage addTarget:nil action:@selector(DishImageClicked:) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:btnImage];
    btnImage.tag = indexPath.row;
    NSLog(@"image clicked at index :%i",btnImage.tag);

}



// Customize the appearance of table view cells.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *strCellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strCellIdentifier];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] 
                 initWithStyle:UITableViewCellStyleDefault 
                 reuseIdentifier:strCellIdentifier] autorelease];

        //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    // assign text for the table view 
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {

        cell.textLabel.text = [arrSearhResults objectAtIndex:indexPath.row];
        cell.textLabel.font = [UIFont boldSystemFontOfSize:14];

        [self configureCell:cell atIndexPath:indexPath];

    }

    else      
    {
        // add image button 
        [self configureCell:cell atIndexPath:indexPath];
        cell.textLabel.text = [self.arrAllItems objectAtIndex:indexPath.row];
        cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
             }
    return cell;
}

【问题讨论】:

  • 请发布 configureCell 方法,我认为问题在于该方法。

标签: iphone objective-c xcode uitableview


【解决方案1】:

这是因为单元格被重复使用,需要在满足if (tableView == self.searchDisplayController.searchResultsTableView)条件时清除内容。

如果您可以引用单元格内的图像视图或按钮,只需清除图像即可。

【讨论】:

  • 我无法根据其数据数组获取搜索到的图像数组,以便在显示搜索到的屏幕后显示它。
  • 我的概率没有重复使用单元格,我的概率实际上是在搜索后为正确的数据获取正确的图像。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-04
相关资源
最近更新 更多