UITableView中的最后一个索引单元格采用了错误的字体

时间:2022-09-07 11:04:07

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.

我有以下代码,乍一看是微不足道的。我只是设置想要将字体类型设置为格鲁吉亚,如果单元格来自搜索结果或者我的学生数组中的计数为零,则大小为14。

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?

但是,这个特殊的代码单元格在我的tableView中是最后一个采用大小为14的格鲁吉亚字体。所有其他单元格正常工作。我的代码中的逻辑错误在哪里?

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

似乎正在发生的事情是当视图控制器被实例化并推送到导航控制器的堆栈顶部时,我的studentsSearch数组为零。我在那个控制器中填充它。

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.

因此,在初始化时,单元格的字体设置为格鲁吉亚,大小为14,因为计数小于0.但是,一旦我填充了studentsSearch数组并重新加载tableView的数据,字体似乎从视图第一次得到初始化。

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

我想现在我需要找到如何将字体设置回该单元格的默认值。

3 个解决方案

#1


2  

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.

我不太确定你在问什么,但我注意到你只有在搜索结果时才将字体设置为格鲁吉亚14;否则,你会忽略它。如果你有一个单元格,在第二个if / then分支中设置了它的字体,然后检索那个单元格(使用dequeueReusableCellWithIdentifier :),它就已经有了它的字体集。

The simplest solution is to add

最简单的解决方案是添加

cell.font = [UIFont systemFontOfSize: 14];

after

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

in the first branch.

在第一个分支。

#2


2  

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.

请记住,表格单元是可回收的。假设您的表有15个可见行。这意味着你有大约15个单元格(或更多单元格)被创建,正如我所说,重新编写。即使你的表有数百行,它仍然会使用相同的15个单元格。

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

因此,如果您的studentsSearch计数> 0,您需要确保将字体大小设置为您的基线(17?)。

#3


1  

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

#1


2  

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.

我不太确定你在问什么,但我注意到你只有在搜索结果时才将字体设置为格鲁吉亚14;否则,你会忽略它。如果你有一个单元格,在第二个if / then分支中设置了它的字体,然后检索那个单元格(使用dequeueReusableCellWithIdentifier :),它就已经有了它的字体集。

The simplest solution is to add

最简单的解决方案是添加

cell.font = [UIFont systemFontOfSize: 14];

after

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

in the first branch.

在第一个分支。

#2


2  

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.

请记住,表格单元是可回收的。假设您的表有15个可见行。这意味着你有大约15个单元格(或更多单元格)被创建,正如我所说,重新编写。即使你的表有数百行,它仍然会使用相同的15个单元格。

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

因此,如果您的studentsSearch计数> 0,您需要确保将字体大小设置为您的基线(17?)。

#3


1  

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