如何从按钮单击的表格视图中删除数据?
问题描述:
我怎样才能从我的桌子上点击按钮点击数据。 我在我的表格视图中添加了5张图片。下次我只添加3张图片,但最后的4张和5张图片不会从我的表格视图中删除。任何人都可以为我提供一个很好的方法来做到这一点。如何从按钮单击的表格视图中删除数据?
我的代码是
(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 = [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];
[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];
}
,并点击一个按钮我从我的数组中删除所有对象。并只添加3个图像。 然后调用myTableView.reloadData();
但前三个正在改变,剩下的4个& 5仍然没有删除。
答
只是一种预感,但你可能要检查您的tableView的实现:numberOfRowsInSection:
请后,如果您还是有问题。
numberOfSectionsInTableView是1 – Christina 2011-06-10 17:45:13
您正在第一行中添加图像0到3(前4个图像)。第二排第四张图片。我们得到的印象是共有5排。这些图像由于其帧分配在另一个之下而以单独的单元格出现在另一个之下。基本上,这些图像根本不会被添加到单独的行中。 – 2011-06-10 17:53:54
和您从imageArray中删除项目,而不仅仅是将值设置为零?然后再调用reloadData,对吗? – picciano 2011-06-10 17:55:55