如果我理解你的问题是正确的,你想要这样的东西(固定位置):
Label1 Label2 Label5 Button1
宁可这样显示(中间标签隐藏时的动态位置):
Label1 Label2 Label5 Button1
无需为每个可能的场景设置框架?
在那种情况下,我可能有一个想法你可以试试。据我所知,标签和按钮(以及 textView 或任何你想使用的东西)都继承自 UIView,它具有框架属性。我的想法是,当您知道要显示哪些标签和按钮时,您可以将它们放在一个数组中,将隐藏的那些排除在外,并设置相对于彼此的框架。像这样:
UILabel *label1 = ...;
UILabel *label2 = ...;
UILabel *label3 = ...;
UILabel *label4 = ...;
UIButton *button1 = ...;
//Let's say you later figure out you want to show labels 1 and 2, but not 3 and 4, but show the button:
NSMutableArray *elements = [[NSMutableArray alloc]init];
[elements addObject:label1];
[elements addObject:label2];
[elements addObject:button1];
//Not adding label3&4
//Now we have collected all the elements we WANT to show togehter in this array.
//Let's give them some good frames
//Loop through the array
for(int i = 0; i<elements.count; i++) //In this case, three times.
{
UIView *element = (UIView*)[elements objectAtIndex:i];
if(i == 0)
{
//Here we set the frame for the first element
//This needs to be treated differently than the others,
//as their positions are relative to this
CGRect previousFrame = element.frame;
[element setFrame:CGRectMake(20, previousFrame.origin.y, previousFrame.size.width, previousFrame.size.height)];
//Setting this first element to start at origin x=20
}
else
{
//If we get here, we know that we're not at the first element
//so we can check where the previous element stopped, which is where we should start
UIView *previousElement = [elements objectAtIndex:i-1];
CGRect pef = previousElement.frame; //PreviousElementFrame
//Let's say we want this element to start exactly where the previous element ended:
CGRect pf= element.frame; //PreviousFrame
[element setFrame:CGRectMake(pef.origin.x + pef.size.width + 20, pf.origin.y, pf.size.width, pf.size.height)];
//I added +20 in origin.x to have some space so they're not completely stuck to eachother
}
}
我使用的原因CGRect previousFrame = element.frame;
是宽度和高度属性保持它们应该是的(如果所有元素都显示,固定位置和大小,它们会是什么)。在某些用例中,我可以想象宽度和高度尚未正确设置,因此您可能会在那里遇到一些奇怪的结果。您可以通过在此循环之前单独设置所有元素的宽度和高度来解决此问题。我想一个按钮比一个标签高,和 textViews 一样,这也意味着如果它们要水平对齐,它们也会有不同的frame.origin.y
值。不同的高度,来自不同的origin.y。您也可以通过在 中添加另一个 if 语句来解决此问题else
,例如if([element isKindOfClass:[UIButton class]]){//setWidthAndHeightForTheButton} else if([element isKindOfClass:[UITextView class]]){//setWidthAndHeightForAllTextViews} else{//setWidthAndHeightForLabels}
,但这一切都取决于您使用的元素(标签、按钮、文本视图、文本字段等)。
请注意,我现在无法对此进行测试,并且我是“手工”编写的,因此这里可能存在错误。正如我所说,这只是一个可行的想法。我假设您正在使用带有所有这些标签的自定义 tableViewCell,因此要为 tableView 执行此操作,您可以将我编写的所有内容放入一个方法中CustomCell.m
,然后从以下位置调用它cellForRowAtIndexPath:
:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//blah blah
cell.nameLabel.text = @"Fidel";
cell.addressLabel.text = @"Cashflow City";
cell.instructionsLabel.text = @""; //empty
cell.paymentLabel.text = @""; //empty
cell.thatButton.hidden = NO;
//Now we don't want the empty labels to use valuable 'invisible' space.
[cell arrangeElements];
}
在customCell.h
:
-(void)arrangeElements;
在customCell.m
:
-(void)arrangeElements
{
NSMutableArray *elements = [[NSMutableArray alloc]init];
if(![nameLabel.text isEqualToString:@""]) // if this label is NOT empty
[elements addObject:nameLabel];
//Repeat above for all elements
//then put the rest of the code from above
}
哇,我真的被这个冲昏了头脑。我想念我用来编程的电脑。。