0

我只是在我的应用程序中实现一个表格视图。我已经多次这样做了,但这次表格单元格显示重复数据并且表格滚动的速度也不好。只是发布我的表格代码。任何有助于防止单元格数据重复和滚动速度优化的帮助将不胜感激。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    int count = [array count];

    if (count%3 == 0)
    {
        return count;
    }
    else
    {
        count = count/3+1;
    }
    NSLog(@"Row count is%i",count);
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"MyIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:MyIdentifier];
    }
    UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 490, 221)];
    int x = 15;
    int y = 15;
    int p = 10;
    int q = 10;
    for (int i = 0; i < 3; i++)
    {
        if ( indexPath.row*3+i < [array count] )
        {
            UIImageView *img = [[UIImageView alloc] init];
            UIButton *btn = [[UIButton alloc] init];
            [img setImageWithURL:[NSURL URLWithString:[array objectAtIndex:indexPath.row*3+i]]
                placeholderImage:[UIImage imageNamed:@"India.png"]
                         success:^(UIImage *image) {}failure:^(NSError *error){}];
            img.frame = CGRectMake(x, y, 140, 201);
            btn.frame = CGRectMake(p, q, 150, 211);
            btn.tag = indexPath.row*3+i;

            [btn setBackgroundImage:[UIImage imageNamed:@"Picframe_N.png"] forState:UIControlStateNormal];
            [btn setBackgroundImage:[UIImage imageNamed:@"Picframe_S.png"] forState:UIControlStateHighlighted];
            [btn addTarget:self action:@selector(Movie_detail:) forControlEvents:UIControlEventTouchUpInside];
            [testView addSubview:img];
            [testView addSubview:btn];

            x = x+160;
            p = p+160;
        }
        else
        {
            NSLog(@"No data");
            break;
        }
    }
    [cell addSubview:testView];
    return cell;
}

单元格中更新的内容是为了在单个单元格中添加 3 个按钮。没有其他的。

4

5 回答 5

2

建议

使用自定义单元格

修复

创建和添加子视图将包含在分配单元格的大括号内

IE

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:MyIdentifier];
         //CREATING AND ADDING SUBVIEW SHOULD BE HERE
    }
          //Change in values is taken care here

原因

否则每次创建子视图时,分配所有对象并在表格滚动时添加到上面

参考

苹果文档

于 2013-04-24T07:17:12.810 回答
1

您面临的问题是您将 UIView 添加到进入屏幕的每个单元格。但是,您正在重用单元格,因此旧的“添加”视图仍在它们上。数据的重复是因为滚动时一个单元格上有多层测试视图。

您应该创建一个包含您在每个 cellforrow 添加的 testview 的 CustomCell:或者当单元格仍然是 nil 值时将 testview 添加到 cellviews 中,这样如果它被重用,您就不会再添加另一个 testview 到单元格,它们重叠。这些示例写在此处的其他答案中,但在网络上随处可见。

不推荐,但:另一个快速但肮脏的修复方法是从单元格中删除所有子视图(或您添加 testview 的任何子视图)for(UIView *v in [cell(.contentView) subviews]){ [v removeFromSuperView]; }

于 2013-04-24T08:29:41.950 回答
0

你好这个问题在你使用的时候是正常的dequeueReusableCell

只需按照我在下面的方法中列出的更改..

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"MyIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    UIView *testView;
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:MyIdentifier];
        testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 490, 221)];
        testView.tag = 111;
        [cell addSubview:testView];
        [testView release];//For Non ARC
    }
    else
    {
        testView = [cell.contentView viewWithTag:111];
        for(UIView * vv in [testView subViews])
        {
            [vv removeFromSuperView];
        }
    }
    int x = 15;
    int y = 15;
    int p = 10;
    int q = 10;
    for (int i = 0; i < 3; i++)
    {
        if ( indexPath.row*3+i < [array count] )
        {
            UIImageView *img = [[UIImageView alloc] init];
            UIButton *btn = [[UIButton alloc] init];
            [img setImageWithURL:[NSURL URLWithString:[array objectAtIndex:indexPath.row*3+i]]
                placeholderImage:[UIImage imageNamed:@"India.png"]
                         success:^(UIImage *image) {}failure:^(NSError *error){}];
            img.frame = CGRectMake(x, y, 140, 201);
            btn.frame = CGRectMake(p, q, 150, 211);
            btn.tag = indexPath.row*3+i;

            [btn setBackgroundImage:[UIImage imageNamed:@"Picframe_N.png"] forState:UIControlStateNormal];
            [btn setBackgroundImage:[UIImage imageNamed:@"Picframe_S.png"] forState:UIControlStateHighlighted];
            [btn addTarget:self action:@selector(Movie_detail:) forControlEvents:UIControlEventTouchUpInside];
            [testView addSubview:img];
            [testView addSubview:btn];

            x = x+160;
            p = p+160;
        }
        else
        {
            NSLog(@"No data");
            break;
        }
    }

    return cell;
}

尝试这个....

于 2013-04-24T07:25:44.187 回答
0

在不同的单元格中重复数据可能是由于相同的“CellIdentifier”,请尝试为不同的单元格视图类型使用不同的“CellIdentifier”。我可以看到不同的单元格视图类型,因为您的代码中有一些条件块。

static NSString *MyIdentifier;

MyIdentifier = @"Type1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:MyIdentifier];
    }


MyIdentifier = @"Type2";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:MyIdentifier];
    }
于 2013-04-24T07:29:29.700 回答
0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"MyIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:MyIdentifier];

        UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 490, 221)];
    int x = 15;
    int y = 15;
    int p = 10;
    int q = 10;
    for (int i = 0; i < 3; i++)
    {
        if ( indexPath.row*3+i < [array count] )
        {
            UIImageView *img = [[UIImageView alloc] init];
            UIButton *btn = [[UIButton alloc] init];
            [img setImageWithURL:[NSURL URLWithString:[array objectAtIndex:indexPath.row*3+i]]
                placeholderImage:[UIImage imageNamed:@"India.png"]
                         success:^(UIImage *image) {}failure:^(NSError *error){}];
            img.frame = CGRectMake(x, y, 140, 201);
            btn.frame = CGRectMake(p, q, 150, 211);
            btn.tag = indexPath.row*3+i;

            [btn setBackgroundImage:[UIImage imageNamed:@"Picframe_N.png"] forState:UIControlStateNormal];
            [btn setBackgroundImage:[UIImage imageNamed:@"Picframe_S.png"] forState:UIControlStateHighlighted];
            [btn addTarget:self action:@selector(Movie_detail:) forControlEvents:UIControlEventTouchUpInside];
            [testView addSubview:img];
            [testView addSubview:btn];

            x = x+160;
            p = p+160;
        }
        else
        {
            NSLog(@"No data");
            break;
        }
    }
    [cell addSubview:testView];
    }

    return cell;
}

你不应该在你cell被初始化的块之外初始化任何东西,因为当你scrolltableview方法cellForRowAtIndexPath每次都被调用时,你的视图testview每次都会被创建。

在单元格初始化之外,您应该只更新要在单元格中显示的值。

希望上面的作品..

已编辑 但更好的方法我建议尝试创建一个Custom Cell,以便您可以轻松访问添加到单元格的组件,以在表格视图滚动时更新它们的值..

谢谢。

于 2013-04-24T07:19:06.860 回答