ios - UITableView 显示与数据源相比不一致的行数

标签 ios objective-c uitableview cocoa-touch

我试图使 UITableView 呈现一个部分的确定行数,但即使我验证其数据源为 numberOfRowsInSection 返回 x 行数, TableView 也显示 x-1。

此意外行为的异常(exception)情况是 numberOfRowsInSection 小于 3。

我什至在 cellForRowAtIndexPath 中放置了一个断点,并且我确认正在为未出现的行调用它。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == SectionNumber_One) {
        return 6;
    } else {
        return self.numberOfProjectRows; // This returns x, but x-1 rows are being shown
    }
}

例如,如果 self.numberOfProjectRows 为 5,则第二部分仅显示 4 行。

如果我手动将其增加到 6,它会显示 5 行,但应该位于第 5 个位置的数据却不存在。

这似乎与屏幕尺寸无关,因为我在 iPad 上进行了测试,结果相同。

为什么会发生这种情况?节中的行数是否还有其他可能的修饰符?

如果有任何帮助,我会附上屏幕截图。

UITableView screenshot

编辑-这是我的委托(delegate)方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // Cell with reuse identifier setup

    if (section == SectionNumber_One) {
        // cell setup for section one that's showing up ok
    } else if (section == SectionNumber_Two) {
        UITextField *projectField = cell.projectTextField;
        if ([self.userProjectKeys count] > row) {
            projectField.text = self.availableProjects[self.userProjectKeys[row]];
        }
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Hide the password row for existing users
    if (indexPath.row == FieldTag_Password && ![self.user.key vol_isStringEmpty]) {
        return 0.0f;
    } else {
        return UITableViewAutomaticDimension;
    }
}

最佳答案

问题可能不在您的数据源方法中,而是在您的Delegate 方法中,tableView(_:heightForRowAt:) .

如果您没有为单元格返回正确的高度,它就不会显示。

即使您在数据源中写入 1000 个单元格也没关系。如果您不返回高度,它们将不会显示。

关于ios - UITableView 显示与数据源相比不一致的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42214968/

相关文章:

ios - svg 中的链接在 iOS 上不起作用

ios - 使用自动布局使 3 个 UIbuttons 居中对齐

ios - 在有限的 textField 长度上剪切粘贴的文本

json - 在 swift 中从 JSON 填充 tableview 时的约定是什么?

objective-c - 如何在我的自定义 TableviewCell 中访问 ImageView 而无需创建其导出

ios - UILabel 垂直对齐

ios - UITableView 删除行。 swift 2

iphone - UIScrollView 的 zoomScale 属性在缩放弹跳时不更新

ios - Facebook iOS SDK 3.5.1 : openActiveSessionWithReadPermissions - completion handler called twice

ios - 复制 iOS 邮件撰写界面的行为,但 View 不跟随光标