简体   繁体   中英

Last indexed cell in UITableView is taking on wrong font

I have the following code which is trivial at first sight. I simply set want to set the font type to Georgia with a size of 14 if the cell is from the result of a search or if there is a count of zero in my students array.

However, with this particular code cell that's last in my tableView is taking on the font of Georgia with size 14. All other cells are working proper. Where in my code is the logic wrong?

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = [indexPath row];
    NSInteger section = [indexPath section];

    static NSString *CellIdentifier = @"Student";
    cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    // Configure the cell
    if([studentsSearch count] > 0) {
        cell.text = (NSString *)[[[studentsSearch objectAtIndex:section] objectAtIndex:row] valueForKey:@"name"];
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    } else {
        if(isSearching == YES) 
            cell.text = @"No students available.";
        else 
            cell.text = @"No students have been added for this school.";

        cell.font = [UIFont fontWithName:@"Georgia" size:14];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}

EDIT

What appears to be happening is when the view controller gets instantiated and pushed on top of the navigation controller's stack, my studentsSearch array is nil . I populate it within that controller.

So upon initialization, the cell has its font set to Georgia with a size of 14 because the count is < 0. However, once I populate the studentsSearch array and reload the tableView 's data, the font seems to be sticking from when the view first got initialized.

I suppose now I need to find how to set the font back to that cell to what the default is.

I'm not quite sure what you're asking, but I do note that you're only setting the font to Georgia 14 when you have a search result; otherwise, you're ignoring it. If you have a cell with it's font set in the second if/then branch, and then retrieve that cell (using dequeueReusableCellWithIdentifier:), it will already have it's font set.

The simplest solution is to add

cell.font = [UIFont systemFontOfSize: 14];

after

    cell.text = (NSString *)[[[...
    cell.accessoryType = ...

in the first branch.

Keep in mind that table cells are recycled. Let's say your table has 15 visible rows. That means you have approximately 15 cells (or a few more) that get created and, as I said, recyled. Even if your table has hundreds of rows, it will still use the same 15 cells.

In this case, you're never resetting the font size, so once you set that font size on a cell, it will be used on any row after it that re-uses the cell.

So, if your studentsSearch count > 0, you need to make sure to set the font size to whatever your baseline is (17?).

I'd suggest that you identify the 'special' cell by giving it a different cell identifier.

In this case, you'd request the special cell with cell reuse identifier, eg @"None", and if cell has not yet been created, then create one and set its font.

This way, you create an extra cell with a special identifier, and it is kept separate from the other regular cells in your table.

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = [indexPath row];
    NSInteger section = [indexPath section];

    static NSString *StudentCellIdentifier = @"Student";
    static NSString *NoneCellIdentifier = @"None";

    // did we find students?
    BOOL found = [studentsSearch count] > 0;

    // get/create correct cell type
    cell = [tv dequeueReusableCellWithIdentifier:(found ? StudentCellIdentifier : NoneCellIdentifier)];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithFrame:CGRectZero 
               reuseIdentifier:(found ? StudentCellIdentifier : NoneCellIdentifier)];
    }

    // return a student, or None cell if no studnts found
    if( found ) 
    {
        cell.text = (NSString *)[[[studentsSearch objectAtIndex:section] objectAtIndex:row] valueForKey:@"name"];
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    } 
    else 
    {
        if(isSearching == YES) 
            cell.text = @"No students available.";
        else 
            cell.text = @"No students have been added for this school.";
        cell.font = [UIFont fontWithName:@"Georgia" size:14];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return [cell autorelease];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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