我创建了imageview以在cellForRowAtIndexPath方法上显示图像。我在Cell中添加了imageview作为子视图。但是当我在那时重新加载我的tableview时,单元格中的图像不会重新加载。以下是我的代码。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[arrcathatname objectAtIndex:indexPath.row]]];
[imgView setFrame:CGRectMake(20, 7, 35, 35)];
[cell addSubview:imgView];
cell.textLabel.text = [arrCatName objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// Configure the cell...
return cell;
}
所以,我想删除我的imageview以正确重新加载图像。
我该怎么做?
答案 0 :(得分:9)
尝试将子视图添加到单元格:
[cell.contentView addSubview:imgView];
您可以使用以下代码删除该子视图:
[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
答案 1 :(得分:0)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
imgView = [[UIImageView alloc] init];
imgView.tag = 111;
[cell.contentView addSubView:imgView];
}
imgView = (UIImageView*)[cell.contentView ViewWithTag:111];
[imgView setFrame:CGRectMake(20, 7, 35, 35)];
imgView.image = [UIImage imageNamed:[arrcathatname objectAtIndex:indexPath.row]];
cell.textLabel.text = [arrCatName objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// Configure the cell...
return cell;
}
试试这个