Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UITableView中最后一个索引单元格的字体错误_Iphone_Objective C_Cocoa Touch - Fatal编程技术网

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

Iphone UITableView中最后一个索引单元格的字体错误,iphone,objective-c,cocoa-touch,Iphone,Objective C,Cocoa Touch,我有以下代码,乍一看很简单。如果单元格来自搜索结果,或者如果我的students数组中的计数为零,我只想将字体类型设置为Georgia,大小为14 然而,我的tableView中最后一个特定的代码单元格采用了大小为14的Georgia字体。所有其他电池都正常工作。在我的代码中,逻辑哪里是错误的 - (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {

我有以下代码,乍一看很简单。如果单元格来自搜索结果,或者如果我的students数组中的计数为零,我只想将字体类型设置为Georgia,大小为14

然而,我的
tableView
中最后一个特定的代码单元格采用了大小为14的Georgia字体。所有其他电池都正常工作。在我的代码中,逻辑哪里是错误的

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

当视图控制器被实例化并推到导航控制器堆栈的顶部时,我的
studentsSearch
数组是
nil
。我将其填充到该控制器中

因此,在初始化时,单元格的字体设置为大小为14的Georgia,因为
计数
小于0。但是,一旦我填充
studentsSearch
数组并重新加载
tableView
的数据,字体似乎从视图首次初始化时就开始粘滞

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

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

最简单的解决方案是添加

cell.font = [UIFont systemFontOfSize: 14];
之后

    cell.text = (NSString *)[[[...
    cell.accessoryType = ...
在第一个分支中。

我不太清楚您在问什么,但我注意到您只有在有搜索结果时才将字体设置为Georgia 14;否则,你就忽略了它。如果在第二个If/then分支中有一个单元格设置了其字体,然后检索该单元格(使用dequeueReusableCellWithIdentifier:),则该单元格将已经设置了其字体

最简单的解决方案是添加

cell.font = [UIFont systemFontOfSize: 14];
之后

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

在第一个分支中。

请记住,表格单元格是循环使用的。假设您的表有15个可见行。这意味着你有大约15个(或更多)细胞被创建,并且,正如我所说的,被重新命名。即使您的表有数百行,它仍将使用相同的15个单元格

在这种情况下,您永远不会重置字体大小,因此,一旦您在单元格上设置了该字体大小,它将用于该单元格之后重新使用该单元格的任何行


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

请记住,表格单元格是循环使用的。假设您的表有15个可见行。这意味着你有大约15个(或更多)细胞被创建,并且,正如我所说的,被重新命名。即使您的表有数百行,它仍将使用相同的15个单元格

在这种情况下,您永远不会重置字体大小,因此,一旦您在单元格上设置了该字体大小,它将用于该单元格之后重新使用该单元格的任何行


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

我建议您通过为“特殊”单元格指定不同的单元格标识符来识别它

在这种情况下,您将请求具有单元重用标识符的特殊单元,例如@“None”,如果尚未创建单元,则创建一个并设置其字体

这样,您就可以创建一个具有特殊标识符的额外单元格,并将其与表中的其他常规单元格分开

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

我建议您通过给它一个不同的单元标识符来识别“特殊”单元

在这种情况下,您将请求具有单元重用标识符的特殊单元,例如@“None”,如果尚未创建单元,则创建一个并设置其字体

这样,您就可以创建一个具有特殊标识符的额外单元格,并将其与表中的其他常规单元格分开

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

嗨,本。基本上,我只是尝试在搜索结果为空或studentsSearch数组中没有学生时设置字体。这就是为什么他们在那个街区。这里的问题是,即使我说10个学生,第10个学生仍然使用乔治亚字体。我还注意到,在抛出一些任意的NSLog调用后,在tableView的第一次加载时,其中一个单元格字体确实更改为乔治亚字体。在我填充studentsSearch数组并重新加载表之后,这个单元格似乎被重新使用,因此字体被更改……这就是为什么您需要将其字体重新设置为您在其他地方使用的字体。另一种选择是使用两个不同的单元格标识符,一个用于搜索,另一个用于其他。听起来不错。现在我只需要弄清楚如何拍摄所使用的原始字体的快照,并在需要时再次调用。您可以随时将其记录到控制台,但我认为可能是[UIFont boldSystemFontOfSize:16]Hi Ben。基本上,我只是尝试在搜索结果为空或studentsSearch数组中没有学生时设置字体。这就是为什么他们在那个街区。这里的问题是,即使我说10个学生,第10个学生仍然使用乔治亚字体。我还注意到,在抛出一些任意的NSLog调用后,在tableView的第一次加载时,其中一个单元格字体确实更改为乔治亚字体。在我填充studentsSearch数组并重新加载表之后,这个单元格似乎被重新使用,因此字体被更改……这就是为什么您需要将其字体重新设置为您在其他地方使用的字体。另一种选择是使用两个不同的单元格标识符,一个用于搜索,另一个用于其他。听起来不错。现在我只需要弄清楚如何拍摄正在使用的原始字体的快照,并在需要时再次调用它。您可以随时将其记录到控制台,但我认为它可能是[UIFont boldSystemFontOfSize:16]