[英]Table view cell images change after searching
I am working on table view cell containing different images on accessory view and data(text)on left side for each cell.Initially all cells are occupied by its relevant data and images.But when i search on searchdisplaycontroller ,the images are allocated as in new array. 我正在工作的表格视图单元格包含附件视图上的不同图像以及每个单元格左侧的数据(文本)。最初所有单元格都被其相关的数据和图像占据。但是当我在searchdisplaycontroller上搜索时,图像的分配方式与新数组。 for ex ;I have table view rows containing images of train ,bus ,airplane with the respective texts.But when i search for Bus ,text appears fine but imageview shows irrelevant image (say image of train). 例如;我有表格视图行,其中包含火车,公共汽车,飞机的图像以及相应的文本。但是当我搜索公共汽车时,文本看起来不错,但是imageview显示了不相关的图像(例如火车的图像)。 What may be the logic to achieve the above requirement.Im using following code to configure the cell 达到上述要求的逻辑可能是什么。我使用以下代码配置单元
- (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;
}
This is because the cells are reused, you need to clear the contents when the condition if (tableView == self.searchDisplayController.searchResultsTableView)
is met. 这是因为单元已被重用,如果满足条件if (tableView == self.searchDisplayController.searchResultsTableView)
则需要清除内容。
If you could reference the imageview or the button inside your cell, just clear the image. 如果您可以引用imageview或单元格内的按钮,则只需清除图像即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.