1

cellForRowAtIndexPath メソッドで画像を表示する imageview を作成しました。サブビューとしてCellにimageviewを追加します。しかし、その時点でテーブルビューをリロードすると、セル内の画像がリロードされません。以下は私のコードです。

- (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;
}

そのため、イメージビューを削除してイメージを適切にリロードしたいと考えています。

どうやってやるの?

4

2 に答える 2

9

サブビューを次のようにセルに追加してみてください:

[cell.contentView addSubview:imgView];

コードを使用してそのサブビューを削除できます:

[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
于 2012-12-13T09:01:28.293 に答える
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;
}

これを試して

于 2012-12-13T08:55:29.427 に答える