views:

1787

answers:

3

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.

+2  A: 

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.

Ben Gottlieb
Hi Ben. I'm basically trying to set the Font only for when a search result is empty or when there are zero students in the studentsSearch array. That's why they're in that else block. The problem here is, even when I have say 10 students, the 10th student still takes on the Georgia font.
Coocoo4Cocoa
I also noticed after throwing in some arbitrary NSLog invocations that upon the tableView's first load, one of the cells font is indeed changed to the Georgia font. It seems this cell is being re-used once I populate the studentsSearch array and reload the table, hence the font being changed.
Coocoo4Cocoa
...and that's why you need to re-set it's font back to whatever you're using elsewhere. The other alternative is to use two different cell identifiers, one for search, one otherwise.
Ben Gottlieb
Sounds about right. Now I just need to figure out how to take a snapshot of the original font being used and call for it again when needed.
Coocoo4Cocoa
You could always log it to the console, but i think it's probably [UIFont boldSystemFontOfSize: 16]
Ben Gottlieb
+2  A: 

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?).

August
+1  A: 

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, e.g. @"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];
}
Jonathan Watmough