【发布时间】:2011-12-05 06:06:17
【问题描述】:
我有带有标签的滚动视图和一个表格视图和一个按钮。我只想滚动滚动视图,表格视图必须显示所有内容,但表格视图不能滚动。在 tableview 下方我有一个按钮。如何设置按钮的框架,使其正好位于表格下方?
谢谢
【问题讨论】:
标签: iphone uitableview uiscrollview
我有带有标签的滚动视图和一个表格视图和一个按钮。我只想滚动滚动视图,表格视图必须显示所有内容,但表格视图不能滚动。在 tableview 下方我有一个按钮。如何设置按钮的框架,使其正好位于表格下方?
谢谢
【问题讨论】:
标签: iphone uitableview uiscrollview
也许您想设置 YourTableView.userInteractionEnabled = NO?
【讨论】:
是的,我们可以禁用滚动表格视图。 转到->xib->选择表->转到第一个选项卡->取消选择启用滚动。
第二个问题的答案。
将 UiView 放在表格的页脚中,然后将按钮放在要在底部显示的 UIView 中。 它会一直显示在底部。
如果您想以编程方式放置按钮,请在 viewDidload 方法中使用以下代码。
///--------Table Footer is Set here
UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 260, 44)];
UIButton *adddays = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 260, 44)];
[adddays setBackgroundImage:[UIImage imageNamed:@"abcd.png"] forState:UIControlStateNormal];
[adddays addTarget:self action:@selector(buttonaction) forControlEvents:UIControlEventTouchDown];
UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(75, 12, 250, 20)];
[text setBackgroundColor:CLEAR_COLOR];
[text setText:@"Title for your button"];
[text setTextColor:XDARK_BLUE];
text.font=[UIFont fontWithName:@"Arial-BoldMT" size:18.0f];
[footer addSubview:adddays];
[footer addSubview:text];
[table setTableFooterView:footer];
【讨论】:
这是假设您已经为 scrollView、tableView 和 button 创建了 IBOutlets,并将它们适当地连接起来。
我发现记住我们只是在处理 CGRect(origin.y 和 size.height)的 y 值很有用 - 应该在 xib 中设置 x 值。
为了更好地说明我的观点,我已经发表了大量评论,通常我只会在适当的地方发表评论
-(void)viewDidLoad {
[self.tableView setScrollEnabled:NO];
// Get the number of rows in your table, I use the method
// 'tableView:numberOfRowsInSection:' because I only have one section.
int numOfRows = [self.tableView numberOfRowsInSection:0];
// Get the height of your rows. You can use the magic
// number 46 (44 without including the separator
// between rows) for the height of your rows, but because
// I was using a custom cell, I had to declare an instance
// of that cell and exctract the height from
// cell.frame.size.height (adding +2 to compensate for
// the separator). But for the purpose of this demonstration
// I'm going to stick with a magic number
int rowHeight = 46; //Eww, Magic numbers! :/
// Get a reference to the tableViews frame, and set the height
// of this frame to be the sum of all your rows
CGRect frame = self.tableView.frame;
frame.size.height = numOfRows * rowHeight;
// Now we have a frame with the exact size of our table,
// so set the 'tableView.frame' AND the 'tableView.contentSize'
// to that. (Because we want ALL rows visible as you
// disabled scrolling for the 'tableView')
self.tableView.frame = frame;
self.tableView.contentSize = frame.size;
// Now we want to set up the button beneath the table.
// We still have the 'frame' variable, which gives us
// the tableView's Y-origin and height. We just add these
// two together (with +20 for padding) to get the origin of the button
CGRect buttonFrame = self.button.frame;
buttonFrame.origin.y = frame.origin.y + frame.size.height + 20;
self.button.frame = buttonFrame;
// Finally, we want the `scrollView`'s `contentSize` to
// encompass this entire setup (+20 for padding again)
CGRect scrollFrame = self.scrollView.frame;
scrollFrame.size.height = buttonFrame.origin.y + buttonFrame.size.height = 20;
self.scrollView.contentSize = scrollFrame.size;
}
【讨论】:
您可以停止滚动表格视图。但是您不应该在滚动视图中添加表格视图。 UITableView 是 UIScrollView 的子类,在另一个上添加一个 scrollView 会产生问题。我建议您删除滚动视图并单独使用表格视图(因为表格视图本身就是滚动视图)。
【讨论】: