我的UITableView顶部不需要的空白UITableViewCell

blu*_*din 5 objective-c uitableview ios

我有一个UITableView,它顶部有一个空白行,我无法弄清楚原因.这是相关的代码,你们有谁知道这里发生了什么?

UITableView加载时没有内容.此方法是在以下情况下启动每个数据刷新的方法:

- (IBAction)updateButton:(id)sender 
{
    if (questionsTextField.isFirstResponder) {
        [questionsTextField resignFirstResponder];
        [self assignQuestionsCount];
    }

    if (currentNumberOfQuestions > 0) {
        // do work calculating
        currentTest = nil;

        currentTest = [self retrieveCurrentTest];
        currentTest.numberOfQuestions = currentNumberOfQuestions;
        currentTest.decimalPlacesToDisplay = 0;
        currentTest.roundingBreakPoint = 0.5;

        currentGradeScale = nil;
        currentGradeScale = [currentTest generateGradingScale];
        [scoresTableView reloadData];
    }
    else {
        // my error handling on text boxes here....
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我对UITableView方法的实现:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.currentGradeScale count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"scoresIndentifier";
    static int missedTag = 1, correctTag = 2, gradeTag = 3;

    UILabel *missedLabel, *correctAndTotalLabel, *letterGradeLabel;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    //if a cell does not exist, get it then initialize it
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

        // populate data
        missedLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100, 50)];
        missedLabel.tag = missedTag;
        missedLabel.font = [UIFont systemFontOfSize:14.0];
        missedLabel.textAlignment = UITextAlignmentCenter;
        missedLabel.textColor = [UIColor blackColor];
        missedLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight |UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
        [cell.contentView addSubview:missedLabel];

    }
    // if it does, just reassign the properties
    else {
        missedLabel = (UILabel *)[cell.contentView viewWithTag:missedTag];
    }

    missedLabel.text = [[self.currentGradeScale objectAtIndex:indexPath.row] determineLetterGrade:0.5];

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

感谢帮助大家,我真的很感激.

gam*_*zii 1

您可能已经考虑过的最明显的解释是表的第一行已设置为空白数据(即 self.currentGradeScale objectAtIndex:0 返回 nil 或 @"" 表示“确定的字母等级 0.5”。)

如果您在调试器中为标签文本赋值的行的 cellForRowAtIndexPath 上放置一个断点,那么它肯定会为第 0 行设置非空/非空值吗?

另请注意,missedLabel 存在内存泄漏 - 将其作为子视图添加到单元格中将保留它,因此您应该在分配时自动释放,或在添加为子视图后释放。