繁体   English   中英

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

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

好吧我不知道为什么这不起作用,但我已经连接了一个tableView,我想要为每个单元格设置19项文本。

单元格填充得很好,但是当我尝试滚动时,它会向下移动,我可以看到在初始视图中看不到的单元格,它会挂起,并且不会向下滚动。 它只是快速回复。 真的很奇怪!

我究竟做错了什么?

- (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的高度

我知道这是一个老问题,这不是你的问题的答案 ,而是改善你的代码以便其他人可以受益的建议。

你可以这样写:

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


而不是这个:

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 = @"";
}


如果您碰巧为每个案例都有不同的文本,您可以使用:

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

或者甚至更好地将所有内容放在NSArray (数据库......)中,然后通过索引循环遍历它们。

这对我有所帮助:

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

@EmptyStack答案是对的。那是替代但不是解决方案。

即使我们可以在故事板中实现相同的目标。

从storyboard控件中选择tableView - >单击“添加缺失约束”。 这将为tableView和View添加约束。

这帮助我解决了这个问题。 附上screen_shot供参考。 解

您分享的代码对我来说很好。 您是否在导航控制器堆栈上使用此tableview推送视图控制器? 我以前见过人们这样做过。 单元格的默认高度为44px,导航栏的高度为44px。 因此,如果您在导航控制器堆栈上按此按钮,则滚动视图会像您所描述的那样快速向后捕捉。 如果是这种情况,请将TableView的高度降低44 px(在“接口”构建器中或以编程方式编写,如果以编程方式绘制)并且应该修复它。

您需要调整UITableView的高度,或者您可以尝试将[tableView reloadData]放在viewDidAppear方法中(如果放在viewWillAppear或viewDidLoad中,它似乎不起作用)。

在大多数情况下,前者适合我。

如果你有故事板或笔尖试试这个(注意,如果你想支持横向,你可能需要添加一些约束)。

在此输入图像描述

我有同样的问题。

检查您看不到的行号。

在viewDidLoad之后,添加以下方法:

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

我希望我一直很有帮助

暂无
暂无

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

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