微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在xcode中重新加载表格视图

如何解决在xcode中重新加载表格视图

| 我的应用程序中有一个带有按钮和表格视图的文本框。在此表格视图中,我正在创建将显示一些图像的图像视图。现在,当我在文本框中输入数字说5时,然后单击按钮再按5图像显示在表格视图中,其中第一行显示4张图像。现在出现的问题是,如果我输入的数字小于5,则说3,那么我只需要在表格视图中获得3张图像,但仍然获得5张图像如果我输入的数字多于5,则我在表格视图上得到所需的数字。我的代码是:-
if([imageArray count] >0)
{
    [imageArray removeAllObjects];
}

int j = [text.text intValue];

for(int i=0;i<j;i++)
{
   [imageArray addobject:[UIImage imageNamed:[Nsstring stringWithFormat:@\"%d.png\",i]]];
}

[tableView reloadData];     

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
   }
   // Customize the appearance of table view cells.
   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


static Nsstring *CellIdentifier = @\"Cell\";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}


if(indexPath.row == 0)
{
    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0,5,100,125)];

    if([imageArray count]>0)
    {

        imageView1.image=[UIImage imageNamed:@\"CARRY.png\"];
        imageView1.image = [imageArray objectAtIndex:0];
        [cell.contentView addSubview:imageView1];
        [imageView1 release];
    }
    if([imageArray count]>1)
    {

        UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(150,125)];
        imageView2.image = [imageArray objectAtIndex:1];
        [imageView2 addSubview:button6];
        [cell.contentView addSubview:imageView2];
        [imageView2 release];
    }
    if([imageArray count]>2)
    {
        UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(310,125)];
        imageView3.image = [imageArray objectAtIndex:2];
        [cell.contentView addSubview:imageView3];
        [imageView3 release];
    }
    if([imageArray count]>3)
    {
        UIImageView *imageView4 = [[UIImageView alloc] initWithFrame:CGRectMake(460,125)];
        imageView4.image = [imageArray objectAtIndex:3];
        [cell.contentView addSubview:imageView4];
        [imageView4 release];
    }

}
else if(indexPath.row == 1)
{
    if([imageArray count]>4)
    {
        UIImageView *imageView5 = [[UIImageView alloc] initWithFrame:CGRectMake(0,125)];
        imageView5.image = [imageArray objectAtIndex:4];
        [cell.contentView addSubview:imageView5];
        [imageView5 release];
    }
    if([imageArray count]>5)
    {                           
        UIImageView *imageView6 = [[UIImageView alloc] initWithFrame:CGRectMake(150,125)];
        imageView6.image = [imageArray objectAtIndex:5];
        [cell.contentView addSubview:imageView6];
        [imageView6 release];
    }

    if([imageArray count]>6)
    {
        UIImageView *imageView7= [[UIImageView alloc] initWithFrame:CGRectMake(310,125)];
        imageView7.image = [imageArray objectAtIndex:6];
        [cell.contentView addSubview:imageView7];
        [imageView7 release];
    }
    if([imageArray count]>7)
    {                           
        UIImageView *imageView8 = [[UIImageView alloc] initWithFrame:CGRectMake(460,125)];
        imageView8.image = [imageArray objectAtIndex:7];
        [cell.contentView addSubview:imageView8];
        [imageView8 release];
    }
}

else if(indexPath.row == 2)
{
    if([imageArray count]>8)
    {
        UIImageView *imageView8 = [[UIImageView alloc] initWithFrame:CGRectMake(0,125)];
        imageView8.image = [imageArray objectAtIndex:8];
        [cell.contentView addSubview:imageView8];
        [imageView8 release];
    }
    if([imageArray count]>9)
    {                           
        UIImageView *imageView8 = [[UIImageView alloc] initWithFrame:CGRectMake(150,125)];
        imageView8.image = [imageArray objectAtIndex:9];
        [cell.contentView addSubview:imageView8];
        [imageView8 release];
    }                           

}




return cell;

}
我正在清理数组并再次向其中添加图像,然后重新加载表格视图,但是为什么输出却没有根据需要而来。请帮助。 谢谢, 克里斯蒂     

解决方法

        问题在于您要添加图像视图,但不会删除先前添加的图像视图,以防重复使用单元格。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。