插入&拖动时从UIScrollView中删除子视图

时间:2013-10-20 11:00:29

标签: objective-c uiscrollview

我根据从数据库中获取的数组计数,在UIButton中将多个UIScrollView(作为卡片)作为子视图放置。除此之外,我能够成功开发无限滚动&它工作正常。所以我有一个名为“记住”的选项。一旦我点击记住,卡片(UIButton作为子视图)必须在滚动时显示较少。我怎样才能实现这一点,即特定索引处的数组必须在UIScrollView中显示较少。任何逻辑或想法?这是我的代码直到无限滚动:

-(void)createCards
{     
[self addCardWithName:[NSString stringWithFormat:@"%@",[entryArray objectAtIndex:      
([entryArray count]-1)]] atPosition:0];

for (int i = 1; i < ([entryArray count]+1); i++) {
    NSString *titleString = [NSString stringWithFormat:@"%@",[entryArray objectAtIndex:i-1] ];
    UIButton *buttonView1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [buttonView1 setFrame:CGRectMake(i*320, 0, 320, 180)];
    [buttonView1 setTitle:titleString forState:UIControlStateNormal];
    buttonView1.tag=i;
    [scrollViewForCard addSubview:buttonView1];
}

[self addCardWithName:[NSString stringWithFormat:@"%@",[entryArray objectAtIndex:0]] atPosition:([entryArray count]+1)];

scrollViewForCard.contentSize = CGSizeMake(480*([entryArray count]+1), 180);
[scrollViewForCard scrollRectToVisible:CGRectMake(320,0,320,180) animated:NO];
}

- (void)addCardWithName:(NSString*)titleString atPosition:(int)position
{
 UIButton *buttonView1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonView1 setFrame:CGRectMake(position*320, 0, 320, 180)];
[buttonView1 setTitle:titleString forState:UIControlStateNormal];
[scrollViewForCard addSubview:buttonView1];
}

#pragma mark scroll view-delegate

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
 int randomInt2 = arc4random() % [entryArray count];    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender 
{    
if (sender.contentOffset.x == 0) {
    [sender scrollRectToVisible:CGRectMake((320*[entryArray count]),0,320,180) animated:NO];
}
else if (sender.contentOffset.x == ([entryArray count]+1)*320) {
    [sender scrollRectToVisible:CGRectMake(320,0,320,180) animated:NO];
}
}

1 个答案:

答案 0 :(得分:0)

在记住单击时,您可以删除所有子视图按钮,如下所示:

while ([self.scrollView viewWithTag:[button tag]] != nil)
        [[self.scrollView viewWithTag:[button tag]] removeFromSuperview];

现在,开始不经常显示记住卡片。定义一个与原始数组长度相同的布尔数组。为记住的卡设置YES值,并且将未记住的值设置为阵列上相应位置的值。

我会继续编辑。