0

我使用以下代码为索引路径处的每一行实现单元格:但问题是当我滚动 tableView 时,单元格会在一行中的一个单元格中加载大量 UIImageView *itemimageview,我尝试使用

   for (UIImageView *sView in cell.subviews) {
        [sView removeFromSuperview];
    }

但它会删除一个单元格的所有子视图。如何解决这个问题呢?...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = [indexPath row];
    NSUInteger oldRow = [lastIndexPath row];
    static NSString *CheckMarkCellIdentifier = @"CheckMarkCellIdentifier";

    //dequeueReusableCellWithIdentifier --
    // Returns a reusable table-view cell object located by its identifier.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CheckMarkCellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CheckMarkCellIdentifier] autorelease];

    }
    /*
    for (UIImageView *sView in cell.subviews) {
        [sView removeFromSuperview];
    }
    */
    UIImageView *itemimageview=[[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 232, 54)];
    itemimageview.image=[UIImage imageNamed:[tabsImageArray objectAtIndex:row]];

    itemimageview.userInteractionEnabled = YES;
    [cell.contentView addSubview:itemimageview];
    [itemimageview release];

    UIImageView *dictIcon=[[UIImageView alloc]initWithFrame:CGRectMake(30, 18, 30, 30)];
    dictIcon.image=[UIImage imageNamed:@"dictionary_icon.png"];
    dictIcon.userInteractionEnabled = YES;
    [cell.contentView addSubview:dictIcon];
    [dictIcon release];

    UILabel *dictNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 23, 100, 21)];
    dictNameLabel.text = dictName;
    dictNameLabel.textColor = [UIColor whiteColor];
    dictNameLabel.shadowColor = [UIColor blackColor];
    dictNameLabel.backgroundColor = [UIColor clearColor];
    dictNameLabel.userInteractionEnabled = YES;
    [cell.contentView addSubview:dictNameLabel];
    [dictNameLabel release];


    //cell.textLabel.text = [tabsImageArray objectAtIndex:row];
    cell.accessoryType = (row == oldRow && lastIndexPath != nil) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;

    return cell;
}
4

2 回答 2

0

考虑运行并检查 [view isKindOfClass:[UIImageView class]] 以检查视图是否是要删除的正确视图类型。

还可以考虑使用 UIView 标记属性标记视图,以便您可以添加一次子视图,然后不必重新创建它们并重用。

以下是您将如何执行此操作:

#define ImageViewOneTag 1001
#define ImageViewTwoTag 1002

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)path {
    static NSString *CellID = @"CellID";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];

    UIImageView *imageViewOne = nil;
    UIImageView *imageViewTwo = nil;

    if (cell) {
        // You've caught a reusable cell. Fetch the image views by their tag.

        imageViewOne = (UIImageView *)[cell viewWithTag:ImageViewOneTag];
        imageViewTwo = (UIImageView *)[cell viewWithTag:ImageViewTwoTag];
    } else {
        // You haven't got a reusable cell. Make one, and make and add the image views to the contentView.

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];

        imageViewOne = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 20.0, 20.0)];
        [imageViewOne setTag:ImageViewOneTag];

        imageViewTwo = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 20.0, 20.0, 20.0)];
        [imageViewTwo setTag:ImageViewTwoTag];

        UIView *contentView = cell.contentView;
        [contentView addSubview:imageViewOne];
        [contentView addSubview:imageViewTwo];
    }

    // By this stage, you've either retrieved a reusable cell, or you've made a new one. Either way, imageViewOne and imageViewTwo now have a reference to the views you mean.
    imageViewOne.image = *imageOneForRow*;
    imageViewTwo.image = *imageTwoForRow*;

    return cell;
}
于 2012-04-15T01:41:23.150 回答
0

我建议您使用自定义表格视图单元格,只需创建一个继承 uitableviewcell 的可可触摸类。

于 2014-10-13T07:21:48.267 回答