在xcode中重新加载表视图

时间:2011-06-14 14:21:05

标签: objective-c xcode cocoa-touch ipad uitableview

我的应用程序中有一个带有按钮和表格视图的文本框。在此表格视图中,我创建的图像视图将显示一些图像。现在,当我在文本框中输入一个数字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,5,100,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, 5, 100, 125)];
        imageView3.image = [imageArray objectAtIndex:2];
        [cell.contentView addSubview:imageView3];
        [imageView3 release];
    }
    if([imageArray count]>3)
    {
        UIImageView *imageView4 = [[UIImageView alloc] initWithFrame:CGRectMake(460,5,100,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,5,100,125)];
        imageView5.image = [imageArray objectAtIndex:4];
        [cell.contentView addSubview:imageView5];
        [imageView5 release];
    }
    if([imageArray count]>5)
    {                           
        UIImageView *imageView6 = [[UIImageView alloc] initWithFrame:CGRectMake(150,5,100,125)];
        imageView6.image = [imageArray objectAtIndex:5];
        [cell.contentView addSubview:imageView6];
        [imageView6 release];
    }

    if([imageArray count]>6)
    {
        UIImageView *imageView7= [[UIImageView alloc] initWithFrame:CGRectMake(310, 5, 100, 125)];
        imageView7.image = [imageArray objectAtIndex:6];
        [cell.contentView addSubview:imageView7];
        [imageView7 release];
    }
    if([imageArray count]>7)
    {                           
        UIImageView *imageView8 = [[UIImageView alloc] initWithFrame:CGRectMake(460,5,100,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,5,100,125)];
        imageView8.image = [imageArray objectAtIndex:8];
        [cell.contentView addSubview:imageView8];
        [imageView8 release];
    }
    if([imageArray count]>9)
    {                           
        UIImageView *imageView8 = [[UIImageView alloc] initWithFrame:CGRectMake(150,5,100,125)];
        imageView8.image = [imageArray objectAtIndex:9];
        [cell.contentView addSubview:imageView8];
        [imageView8 release];
    }                           

}




return cell;

}

我正在清理数组并再次向其添加图像然后重新加载表视图,但为什么输出不会根据需要而来。请帮助。

谢谢, 克里斯蒂

1 个答案:

答案 0 :(得分:1)

问题是您正在添加图片视图,但是您没有删除先前在细胞重用时添加的图像视图。