繁体   English   中英

UITableView,滚动后重复的图标

[英]UITableView, icons duplicated after scrolling

我使用以下代码显示特定的UITableView:第一行必须显示一个图标,所有“内部”行都显示一个不同的图标,最后,还有另一个图标。 因此,我们有三个图标,一个用于顶部单元格,一个用于“内部”单元格,一个用于底部单元格。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell"];

    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;   
    }

    TabBarTestAppDelegate *delegate = (TabBarTestAppDelegate *)[[UIApplication sharedApplication] delegate];
    NSArray *local = delegate.myData;
    // ok, it's horrible, don't look at it   :-)
    cell.textLabel.text = [NSString stringWithFormat:@"%@%@", @"       " ,[local objectAtIndex:indexPath.row]];
    //
    NSString* name;
    if (indexPath.row == 0) {
        name = @"topicon";
    }
    else if (indexPath.row + 1 == [local count]) {
        name = @"bottomicon";
    }
    else {
        name = @"innericon";
    }
    //
    UIImage *leftIcon = [[UIImage alloc] initWithContentsOfFile: [[NSBundle mainBundle] pathForResource:name ofType:@"png"]];
    //
    UIImageView *imageView = [[UIImageView alloc] initWithImage:leftIcon];
    [cell addSubview:imageView];
    imageView.frame = CGRectMake(20,0,30,44);
    [leftIcon release];
    [imageView release[;

    return cell;
}

怎么了。 第一次由模拟器加载UITableView时,似乎没有问题,但是在滚动之后 ,它发生了一些奇怪事情: 第一个和最后一个单元格显示了正确的图标,下面是另一个图标。 底部和顶部图标下方的图标是“内部”图标,仅当indexPath.row!=0indexPath.row+1!=[local count]时才应显示。 坦白说,我不知道滚动时是否其他所有图标都重复了,但是这种效果是显而易见的。

我以为子视图可​​能存在一些问题,但是作为一名目标C新手,我无法确切地想出什么。

任何帮助表示赞赏。

好了,TableView正在重用单元格,并且每次显示单元格时都添加图像。

因此,当重复使用单元格时,您将添加另一个图像,但是已经有一个图像。

您将不得不重用图像视图,并且仅在创建单元格时才添加图像。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifer = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];

    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer]autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;   


        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20,0,30,44)];
        imageView.tag = 1001;
        [cell addSubview:imageView];
        [imageView release], imageView= nil;
    }

    TabBarTestAppDelegate *delegate = (TabBarTestAppDelegate *)[[UIApplication sharedApplication] delegate];
    NSArray *local = delegate.myData;
    // ok, it's horrible, don't look at it   :-)
    cell.textLabel.text = [NSString stringWithFormat:@"%@%@", @"       " ,[local objectAtIndex:indexPath.row]];
    //

    NSString* name = nil;;
    if (indexPath.row == 0) {
        name = @"topicon";
    }
    else if (indexPath.row + 1 == [local count]) {
        name = @"bottomicon";
    }
    else {
        name = @"innericon";
    }

    UIImageView *imageView = (UIImageView *)[cell viewWithTag:1001];
    imageView.image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:name ofType:@"png"]];

    return cell;
 }

一样东西。 不要一次又一次地添加新的图像视图。 尝试重用它们。 这里的文章中,我给出了一个简单的示例来重用单元中的组件。

我为此添加了一个解决方案,因为我一直对此持反对态度,而且这里的大多数答案都在2-3岁左右,现在有一种更好的方法来解决这个问题...

在Storyboard编辑器中,您可以将多个原型单元拖到UITableView中,并为它们提供不同的重用标识符,例如:

在此处输入图片说明

if ( specialCase ) {
    UIColor *color = [UIColor colorWithRed:0.2 green:(0.8) blue:0.2 alpha:1.0];
    cell = [tableView dequeueReusableCellWithIdentifier:@"TopCellGreen" forIndexPath:indexPath];
    cell.textColor = color;
} else {
    cell = [tableView dequeueReusableCellWithIdentifier:@"TopCell"  forIndexPath:indexPath];
}

注意不同的标识符。 这样可以防止您的自定义内容(绿色文本)随机出现在[重新]使用同一单元格的其他单元格中。

遇到类似的问题。 我需要在表的最后一个单元格中添加一个图像,其中indexPath.row设置为15。

因此,我使用了Glenn(tnx很多!)所示的技巧,最终得到了这样的结果:

NSString *CellIdentifier = @"Cell";
if (indexPath.row==15)
    CellIdentifier=@"CamCell";
NuovaSegnalazioneCell *cell = (NuovaSegnalazioneCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
    cell = [[NuovaSegnalazioneCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    cell.detailTextField.delegate = self;
    NSLog(@"INIT CELL %d",indexPath.row);
}

这样,第15个单元格具有不同的CellIdentifier,并且在滚动表格时不会在其他任何地方重复使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM