简体   繁体   English

UITableView不滚动(iOS)

[英]UITableView Doesn't Scroll (iOS)

I have created a UITableView programmatically and am finding that it does not scroll. 我已经以编程方式创建了UITableView,并且发现它无法滚动。 This is the first time I've created a UITableView programmatically as opposed to through Interface Builder. 这是我第一次通过编程而不是通过Interface Builder创建UITableView。 Is there some property I need to set programmatically to enable my view to scroll? 我需要以编程方式设置一些属性以使视图滚动吗? I have tried setBounces, setScrollEnabled, setAlwaysBounceVertical, but nothing seems to work. 我已经尝试过setBounces,setScrollEnabled,setAlwaysBounceVertical,但是似乎没有任何效果。 Any ideas would be greatly appreciated! 任何想法将不胜感激! Thanks! 谢谢!

Sample Code: 样例代码:

UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
CGFloat height = (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) ? 460 : 300;
feed = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 44.0f, self.bounds.size.width, height - 44.0f)];
[feed setSeparatorColor:[UIColor colorWithRed:38.0f/255.0f green:43.0f/255.0f blue:63.0f/255.0f alpha:1.0f]];
[feed setBackgroundColor:[UIColor colorWithRed:48.0f/255.0f green:53.0f/255.0f blue:73.0f/255.0f alpha:1.0f]];
[feed setDataSource:self];
[feed setDelegate:self];
[feed setHidden:YES];
[feed setBounces:YES];
[feed setScrollEnabled:YES];
[feed setAlwaysBounceVertical:YES];
[feed setDelaysContentTouches:YES];
[self addSubview:feed];

尝试在TableView上设置delaysContentTouches。

ScrollViews must have their contentSize set in order to scroll. 滚动视图必须设置其contentSize才能滚动。 Try setting your contentSize: 尝试设置您的contentSize:

[feed setContentSize:[feed frame].size];

I tried to put this in a comment but it was too long... 我试图对此发表评论,但时间太长了...

Most of your set methods are just setting the defaults, so they aren't necessary in the final version of your code. 您的大多数set方法只是设置默认值,因此在代码的最终版本中不需要它们。 Also, there is nothing wrong with the way you create the tableview and add it as a subview. 同样,创建表视图并将其添加为子视图的方式也没有错。 This leads me to believe the bug is in surrounding code, maybe having to do with properties on the superview. 这使我相信该错误是在周围的代码中,也许与超级视图上的属性有关。 When you say "working perfectly", do you mean that you can select cells and have your didSelectRow... method called? 当您说“完美地工作”时,是否意味着您可以选择单元格并调用didSelectRow ...方法? You can try to comment things out, stripping the tableview to the bare bones required (init, addSubview, numberOfRows return 0, cellForRow return nil, don't set anything other than the delegate and datasource). 您可以尝试注释掉所有内容,将tableview剥离为所需的基本内容(init,addSubview,numberOfRows返回0,cellForRow返回nil,除了委托和数据源外,请勿设置其他任何内容)。 That should give you an empty tableview that scrolls and bounces even with nothing in it. 那应该给您一个空的表视图,即使其中没​​有任何内容,它也会滚动和跳动。

I know this is an old thread, but are you seeing this problem in the iOS Simulator by any chance? 我知道这是一个旧线程,但是您是否有机会在iOS模拟器中看到此问题? I was having the same problem with my table view, but it turned out that I just wasn't using the right gesture in the simulator. 我的表格视图遇到了同样的问题,但事实证明我只是在模拟器中使用的手势不正确。 See Can't get UITableView list to scroll in iPhone simulator . 请参阅“无法获取UITableView列表以在iPhone模拟器中滚动”

I'm using Xamarin Studio which starts the iOS Simulator. 我正在使用Xamarin Studio,它将启动iOS模拟器。 I had to restart my Mac. 我必须重新启动Mac。 Now the tableview scrolls again. 现在,tableview再次滚动。 Only restarting Xamarin or the iOS Simulator didn't worked. 仅重新启动Xamarin或iOS模拟器无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM