简体   繁体   English

我的UITableView不会向下滚动数据的末尾! 为什么?

[英]My UITableView won't scroll down through the end of the data! Why?

Ok I don't know why this isn't working, but I have hooked up a tableView, with 19 items of text I'd like to set to each cell. 好吧我不知道为什么这不起作用,但我已经连接了一个tableView,我想要为每个单元格设置19项文本。

The cells populate just fine, but when I try and scroll, it goes down there and I can see the cells that aren't visible on the initial view, it hangs, and won't scroll down. 单元格填充得很好,但是当我尝试滚动时,它会向下移动,我可以看到在初始视图中看不到的单元格,它会挂起,并且不会向下滚动。 It just snaps back. 它只是快速回复。 REALLY WEIRD! 真的很奇怪!

What am I doing wrong? 我究竟做错了什么?

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section    {
// Return the number of rows in the section.
return 19;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell...
if(indexPath.row == 0){
    cell.textLabel.text = @"";
}else if(indexPath.row == 1){
    cell.textLabel.text = @"";
}else if(indexPath.row == 2){
    cell.textLabel.text = @"";
}else if(indexPath.row == 3){
    cell.textLabel.text = @"";
}else if(indexPath.row == 4){
    cell.indentationLevel = 2;
    cell.textLabel.text = @"";
}else if(indexPath.row == 5){
    cell.indentationLevel = 2;
    cell.textLabel.text = @"";
}else if(indexPath.row == 6){
    cell.indentationLevel = 2;      
    cell.textLabel.text = @"";
}else if(indexPath.row == 7){
    cell.indentationLevel = 2;      
    cell.textLabel.text = @"";
}else if(indexPath.row == 8){
    cell.indentationLevel = 2;      
    cell.textLabel.text = @"";
}else if(indexPath.row == 9){
    cell.textLabel.text = @"";
}else if(indexPath.row == 10){
    cell.textLabel.text = @"";
}else if(indexPath.row == 11){
    cell.textLabel.text = @"";
}else if(indexPath.row == 12){
    cell.textLabel.text = @"";
}else if(indexPath.row == 13){
    cell.textLabel.text = @"";
}else if(indexPath.row == 14){
    cell.textLabel.text = @"";
}else if(indexPath.row == 15){
    cell.textLabel.text = @"";
}else if(indexPath.row == 16){
    cell.textLabel.text = @"";
}else if(indexPath.row == 17){
    cell.textLabel.text = @"";
}else if(indexPath.row == 18){
    cell.textLabel.text = @"";
}

return cell;
}

降低 tableView的高度

I know it's an old question and this is not an answer to your question, but a suggestion to improve your code so others can benefit. 我知道这是一个老问题,这不是你的问题的答案 ,而是改善你的代码以便其他人可以受益的建议。

You could write this: 你可以这样写:

if (indexPath.row >= 4 && indexPath.row <= 8) { // 4 and 8 inclusive
    cell.indentationLevel = 2;
}
cell.textLabel.text = @"";


Instead of this: 而不是这个:

if(indexPath.row == 0){
    cell.textLabel.text = @"";
}else if(indexPath.row == 1){
    cell.textLabel.text = @"";
}else if(indexPath.row == 2){
    cell.textLabel.text = @"";
}else if(indexPath.row == 3){
    cell.textLabel.text = @"";
}else if(indexPath.row == 4){
    cell.indentationLevel = 2;
    cell.textLabel.text = @"";
}else if(indexPath.row == 5){
    cell.indentationLevel = 2;
    cell.textLabel.text = @"";
}else if(indexPath.row == 6){
    cell.indentationLevel = 2;      
    cell.textLabel.text = @"";
}else if(indexPath.row == 7){
    cell.indentationLevel = 2;      
    cell.textLabel.text = @"";
}else if(indexPath.row == 8){
    cell.indentationLevel = 2;      
    cell.textLabel.text = @"";
}else if(indexPath.row == 9){
    cell.textLabel.text = @"";
}else if(indexPath.row == 10){
    cell.textLabel.text = @"";
}else if(indexPath.row == 11){
    cell.textLabel.text = @"";
}else if(indexPath.row == 12){
    cell.textLabel.text = @"";
}else if(indexPath.row == 13){
    cell.textLabel.text = @"";
}else if(indexPath.row == 14){
    cell.textLabel.text = @"";
}else if(indexPath.row == 15){
    cell.textLabel.text = @"";
}else if(indexPath.row == 16){
    cell.textLabel.text = @"";
}else if(indexPath.row == 17){
    cell.textLabel.text = @"";
}else if(indexPath.row == 18){
    cell.textLabel.text = @"";
}


And if you happen to have different text for each case you could either use: 如果您碰巧为每个案例都有不同的文本,您可以使用:

switch (indexPath.row) {
    case 0:
        cell.textLabel.text = @"";
        break;
        ...
    case 18:
        cell.textLabel.text = @"";
    default:
        break;
}

Or even better to put everything inside an NSArray (database...) and just loop through them by the index. 或者甚至更好地将所有内容放在NSArray (数据库......)中,然后通过索引循环遍历它们。

这对我有所帮助:

[self.tableView setContentInset:UIEdgeInsetsMake(0,0,65,0)];

@EmptyStack answer is right.That was a alternate but NOT the Solution. @EmptyStack答案是对的。那是替代但不是解决方案。

Even we can achieve the same in storyboard. 即使我们可以在故事板中实现相同的目标。

Select tableView --> from the storyboard control click on 'add Missing constraints'. 从storyboard控件中选择tableView - >单击“添加缺失约束”。 That will add the constraint to tableView and View. 这将为tableView和View添加约束。

That helped me to resolve this issue. 这帮助我解决了这个问题。 A screen_shot attached for reference. 附上screen_shot供参考。 解

The code that you shared looks fine to me. 您分享的代码对我来说很好。 Are you pushing view controller with this tableview on navigation controller stack ? 您是否在导航控制器堆栈上使用此tableview推送视图控制器? I have seen people do that before. 我以前见过人们这样做过。 Default height of cell is 44px and height of navigation bar is 44px as well. 单元格的默认高度为44px,导航栏的高度为44px。 So, if you push this on navigation controller stack you scroll view snaps back like you have described. 因此,如果您在导航控制器堆栈上按此按钮,则滚动视图会像您所描述的那样快速向后捕捉。 If that is the case reduce height of TableView by 44 px ( in Interface builder or Programatically if you drew this programatically) and it should fix it. 如果是这种情况,请将TableView的高度降低44 px(在“接口”构建器中或以编程方式编写,如果以编程方式绘制)并且应该修复它。

You need to either adjust the height of you UITableView or you can try to put [tableView reloadData] inside viewDidAppear method (it doesn't seem to work if put inside viewWillAppear or viewDidLoad though). 您需要调整UITableView的高度,或者您可以尝试将[tableView reloadData]放在viewDidAppear方法中(如果放在viewWillAppear或viewDidLoad中,它似乎不起作用)。

The former works for me in most cases. 在大多数情况下,前者适合我。

If you have a storyboard or nib try this (note, you might have to add some constraints if you want to support landscape). 如果你有故事板或笔尖试试这个(注意,如果你想支持横向,你可能需要添加一些约束)。

在此输入图像描述

I had the same problem. 我有同样的问题。

Check the number of the row that you can't see. 检查您看不到的行号。

After in your viewDidLoad, add this method: 在viewDidLoad之后,添加以下方法:

myTableView.contentInset = UIEdgeInsets(top: 0,left: 0,bottom: yourCellHeight * rowHeight,right: 0)

I hope I have been helpful 我希望我一直很有帮助

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

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