Last indexed cell in UITableView is using the wrong font

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

However, with this particular code cell in mine tableView

, it is typing Georgia font with size 14. All other cells are working fine. Where is the wrong logic in my code?

- (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 when the view controller gets an instance and is pushed on top of the navigation controller stack, my array studentsSearch

is nil

. I am filling it inside this controller.

So, on initialization, the cell has its font set in Georgia with size 14 because count

0. However, once I populate the array studentsSearch

and reload the data tableView

, the font seems to stick when the view is initialized first.

I suppose now I need to find how to set the font back to this cell, by default.

+1


source to share


3 answers


I'm not pretty sure what you are asking, but I notice that you only set the font to Georgia 14 when you have a search result; otherwise, you ignore it. If you have a cell with that font set in the second if / then branch, and then retrieve that cell (using dequeueReusableCellWithIdentifier :), it will already have its font installed.

The simplest solution is to add

cell.font = [UIFont systemFontOfSize: 14];

      



after

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

      

in the first branch.

+2


source


Be aware that table cells are being recycled. Let's say your table has 15 visible rows. This means that you have approximately 15 cells (or more) that are created and, as I said, reworked. Even if your table contains hundreds of rows, it will still use the same 15 cells.

In this case, you will never reset the font size, so once you set the font size in a cell, it will be used in any row after reusing the cell.



So if your studentSearch count> 0 then you need to set the font size regardless of the original line (17?).

+2


source


I would assume that you identify the "special" cell by giving it a different cell ID.

In this case, you are requesting a special cell with a cell reuse id, eg. @ "No" and if the cell hasn't been created yet, create it and set its font.

This way you create an extra cell with a special ID and keep it separate from other normal 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];
}

      

+1


source







All Articles