UITableView,滚动后重复的图标 [英] UITableView, icons duplicated after scrolling

查看:45
本文介绍了UITableView,滚动后重复的图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using the following code to show a particular UITableView: the first row must show an icon, all the "inner" rows a different one and, at the end, there's another icon. So, we have three icons, one for the top cell, one for the "inner" cells, and one for the bottom cell.

- (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!= 0 indexPath.row + 1!= [本地数量] .坦白说,我不知道滚动时是否其他所有图标都重复了,但是这种效果还是很明显的.

What happens. The first time the UITableView is loaded by the simulator there's apparently no problem, but, after scrolling, it happens something strange: the first and the last cell show the right icon AND, underneath, another icon. The icon under the bottom and the top icon is an "inner" icon, that should appear only when indexPath.row!=0 or indexPath.row+1!=[local count]. Frankly, I don't know if every other icon gets duplicated while scrolling, but this effect is quite visible.

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

I imagined there could be some issue with the subview, but, being an objective-C newbie, I can't imagine exactly what.

感谢您的帮助.

推荐答案

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

Well the TableView is reusing the cells, and you add the image every time a cell is displayed.

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

Thus when reusing the cell you add an other image, but there already is an image.

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

You will have to reuse the image view, and only add the image if you create the cell.

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

这篇关于UITableView,滚动后重复的图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆