我想创建一个包含添加按钮的主视图。
每次单击添加按钮时,必须在主视图中添加一个子视图。子视图存在于另一个由关闭按钮组成的类中,单击关闭按钮时,子视图类通过委托功能和特定的通知主视图子视图雾从主视图中删除。
剩余的子视图必须在主视图中重新定位。
我希望以矩阵形式(2xN)矩阵将子视图添加到主视图中。也就是说,一行中有 2 个子视图,n 列。
我已经实现了两种相同的方法,但是子视图没有重新定位。
方法一:
- (IBAction)btnAddView:(id)sender
{
if ((count%2)==0)
{
NewSubView *subView = [[NewSubView alloc]initWithFrame:CGRectMake(10, 10+ (i*120), 100, 100) andDelegate:self];
// [self.view addSubview:subView];
[arr addObject:subView];
i++;
}
else if ((count%2)==1)
{
NewSubView *subView = [[NewSubView alloc]initWithFrame:CGRectMake(120, 10+(j*120), 100, 100) andDelegate:self];
// [self.view addSubview:subView];
[arr addObject:subView];
j++;
}
count = count+1;
for (int x=0; x<[arr count]; x++) {
[self.view addSubview:[arr objectAtIndex:x]];
}
self.lblCount.text = [ NSString stringWithFormat:@"count: %d",count];
}
// the delegate function for deleting the view is as follows
- (void) handleDelegate:(id)sender
{
int deletedIndex;
for (int val=0; val<[arr count]; val++) {
if([[arr objectAtIndex:val] isEqual:sender])
deletedIndex =val;
}
dupArray =[[NSMutableArray alloc]initWithArray:arr];
[arr removeObjectAtIndex:deletedIndex];
[sender removeFromSuperview];
count--;
self.lblCount.text = [ NSString stringWithFormat:@"count: %d",count];
if ((deletedIndex%2)==0)
i=deletedIndex/2;
if ((deletedIndex%2)==1)
j--;
}
方法二:
- (IBAction)btnAddView:(id)sender
{
int modifiedColValue= 10+(colValue*120);
if (modifiedColValue<320)
{
NewSubView *subView = [[NewSubView alloc]initWithFrame:CGRectMake(rowValue, modifiedColValue, 100, 100) andDelegate:self];
[arr addObject:subView];
colValue++;
}
else if (modifiedColValue>320)
{
rowValue=10;
[self btnAddView:sender];
colValue++;
}
count++;
for (int x=0; x<[arr count]; x++)
{
[self.view addSubview:[arr objectAtIndex:x]];
}
}
我如何重新定位剩余的子视图,其中 count 是子视图的数量,320 是主视图的宽度。