Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 表单元格的奇怪行为_Iphone_Ios_Objective C_Ipad_Uitableview - Fatal编程技术网

Iphone 表单元格的奇怪行为

Iphone 表单元格的奇怪行为,iphone,ios,objective-c,ipad,uitableview,Iphone,Ios,Objective C,Ipad,Uitableview,我只是在我的应用程序中实现一个表视图。我已经这样做了很多次,但这次表格单元格显示重复数据,表格滚动的速度也不好。只是张贴我的表代码。是否有助于防止单元格数据重复和滚动速度 优化将不胜感激 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { int count = [array count]; if (count%3 == 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;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
int count=[数组计数];
如果(计数%3==0)
{
返回计数;
}
其他的
{
计数=计数/3+1;
}
NSLog(@“行计数为%i”,计数);
返回计数;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*MyIdentifier=@“MyIdentifier”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
如果(单元格==nil)
{
单元格=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
}
UIView*testView=[[UIView alloc]initWithFrame:CGRectMake(0,0490221)];
int x=15;
int y=15;
int p=10;
int q=10;
对于(int i=0;i<3;i++)
{
if(indexPath.row*3+i<[数组计数])
{
UIImageView*img=[[UIImageView alloc]init];
UIButton*btn=[[UIButton alloc]init];
[img setImageWithURL:[NSURL URLWithString:[array objectAtIndex:indexPath.row*3+i]]
占位符图像:[UIImage ImageName:@“India.png”]
成功:^(UIImage*image){}失败:^(NSError*error){}];
img.frame=CGRectMake(x,y,140201);
btn.frame=CGRectMake(p,q,150,211);
btn.tag=indexPath.row*3+i;
[btn setBackgroundImage:[UIImage ImageName:@“Picframe_N.png”]用于状态:UIControlStateNormal];
[btn setBackgroundImage:[UIImage ImageName:@“Picframe_.png”]用于状态:UIControlStateHighlighted];
[btn addTarget:self action:@selector(电影详细信息:)for ControlEvents:UIControlEventTouchUpInside];
[testView addSubview:img];
[测试视图添加子视图:btn];
x=x+160;
p=p+160;
}
其他的
{
NSLog(“无数据”);
打破
}
}
[单元添加子视图:testView];
返回单元;
}

单元格中更新的内容用于在单个单元格中添加3个按钮。没有别的了。

建议

使用自定义单元格

修复

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

原因

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

参考


建议

使用自定义单元格

修复

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

原因

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

参考

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*MyIdentifier=@“MyIdentifier”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
如果(单元格==nil)
{
单元格=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
UIView*testView=[[UIView alloc]initWithFrame:CGRectMake(0,0490221)];
int x=15;
int y=15;
int p=10;
int q=10;
对于(int i=0;i<3;i++)
{
if(indexPath.row*3+i<[数组计数])
{
UIImageView*img=[[UIImageView alloc]init];
UIButton*btn=[[UIButton alloc]init];
[img setImageWithURL:[NSURL URLWithString:[array objectAtIndex:indexPath.row*3+i]]
占位符图像:[UIImage ImageName:@“India.png”]
成功:^(UIImage*image){}失败:^(NSError*error){}];
img.frame=CGRectMake(x,y,140201);
btn.frame=CGRectMake(p,q,150,211);
btn.tag=indexPath.row*3+i;
[btn setBackgroundImage:[UIImage ImageName:@“Picframe_N.png”]用于状态:UIControlStateNormal];
[btn setBackgroundImage:[UIImage ImageName:@“Picframe_.png”]用于状态:UIControlStateHighlighted];
[btn addTarget:self action:@selector(电影详细信息:)for ControlEvents:UIControlEventTouchUpInside];
[testView addSubview:img];
[测试视图添加子视图:btn];
x=x+160;
p=p+160;
}
其他的
{
NSLog(“无数据”);
打破
}
}
[单元添加子视图:testView];
}
返回单元;
}
你不应该在你的
单元格
被初始化的块之外初始化任何东西,因为当你
滚动你的
表格视图
时,方法
cellforrowatinexpath
每次都会被调用,而你的视图
testview
每次都会被创建

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

希望以上的工作

已编辑 但我建议您尝试创建一个
自定义单元格
,这样您就可以轻松访问添加到单元格中的组件,以便在tableview滚动时更新它们的值

谢谢。

-(UITableViewCell*)tableView:(UITableView*)tableView cell for row索引路径:(nsindepath*)indepath
{
静态NSString*MyIdentifier=@“MyIdentifier”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:MyIdentifier];
         //CREATING AND ADDING SUBVIEW SHOULD BE HERE
    }
          //Change in values is taken care here
- (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;
}
- (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;
}
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];
    }