iphone - 表格单元格的奇怪行为

标签 iphone ios objective-c ipad uitableview

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

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

Things updated in cell 是为了在单个单元格中添加 3 个按钮。没有别的。

最佳答案

建议

使用自定义单元格

修复:

创建和添加 subview 要包含在分配单元格的大括号内

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

原因

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

引用

Apple Docs

关于iphone - 表格单元格的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16185572/

相关文章:

objective-c - 给定年份和周数,如何获得一周的第一天的日期?

iphone - iPhone 中的 WEBGL_depth_texture 精度太低

iphone - 何时*不*使用 Core Data 类型 Transformable?

ios - 如何在不通知 NSFetchedResultsController 的情况下更新 NSManagedObject 或 NSManagedObjectContext

ios - 在swift 4中从表中获取数据

javascript - Pushwoosh phonegap 插件,检索设备 ID

ios - Google map URL 方案不适用于 iOS 9

objective-c - 2 年前的代码现在不起作用(WindowController alloc)

iPhone - UITextView 在编辑时改变位置

iphone - 智能应用程序横幅不适用于 iOS