Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unwanted blank UITableViewCell at the top of my UITableView

I have a UITableView that has 1 blank row at the top of it, and I cannot figure out why. Here is the relevant code, do you folks have any idea what's going on here?

The UITableView loads up with no content. This method is what kicks off each data refresh after:

- (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....
    }
}

Here is my implementation of the UITableView methods:

- (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;
}

Thanks for the help folks, I really appreciate it.

like image 989
blundin Avatar asked Nov 05 '22 16:11

blundin


2 Answers

The most obvious explanation which you've probably already considered is that the first row of the table has been set with blank data (i.e. self.currentGradeScale objectAtIndex:0 returns nil or @"" for "determined letter grade 0.5.")

If you put a breakpoint on cellForRowAtIndexPath in the debugger at the line where you assign a value to the label text is it definitely setting a non-null/non-blank value for row 0?

Also side note there is a memory leak on missedLabel - adding it as a subview to the cell will retain it so you should autorelease on alloc, or release after adding as a subview.

like image 54
gamozzii Avatar answered Nov 10 '22 07:11

gamozzii


I had this same problem and found that there was a value in the Scroll View Size / Content Insets / Top area. See attached image. Once I set that to 0 and saved, the blank area at the top went away. I hope this helps.

enter image description here

like image 42
Travis M. Avatar answered Nov 10 '22 05:11

Travis M.