在滚动视图中添加tableview,在Iphone中禁用表滚动

时间:2011-10-11 11:54:46

标签: iphone uitableview uiscrollview

我有带标签的滚动视图,表格视图和按钮。我只想滚动滚动视图,表视图必须显示所有内容,但tableview不能滚动。在tableview下面我有一个按钮。如何设置按钮的框架,使其正好位于桌子下方?

谢谢

4 个答案:

答案 0 :(得分:0)

也许你想设置YourTableView.userInteractionEnabled = NO?

答案 1 :(得分:0)

是的,我们可以禁用滚动tableview。 转到> 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];  

答案 2 :(得分:0)

这假设您已为scrollViewtableViewbutton创建了IBOutlets,并正确地将它们连接起来。

我发现记住我们只是弄乱了CGRectorigin.y& size.height)的y值很有用 - 应该设置x值在xib。

我对此进行了大量评论,以更好地说明我的观点,通常我只会在适当的地方发表评论

-(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;
}

答案 3 :(得分:-1)

您可以停止滚动表格视图。但是你不应该在scrollview中添加tableview。 UITableView是UIScrollView的子类,在另一个上添加一个scrollView会产生问题。我建议你删除scrollview并单独使用tableview(因为tableview本身是一个scrollview)。