[英]Last indexed cell in UITableView is taking on wrong font
我有下面的代码,乍看之下很简单。 如果单元格是来自搜索结果,或者我的学生数组中的计数为零,我只是简单地想将字体类型设置为格鲁吉亚,大小为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,因为count
是<0。然而,一旦予填充studentsSearch
阵列和重新加载tableView
的数据,该字体似乎是从当视图粘首先被初始化。
我想现在我需要找到如何将字体设置回该单元格的默认设置。
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; 我清楚你问什么,但我注意到,你当你有一个搜索结果会只是字体设置格鲁吉亚14; 否则,您将忽略它。 如果您在第二个if / then分支中设置了带有其字体的单元格,然后检索该单元格(使用dequeueReusableCellWithIdentifier :),那么它将已经设置了其字体。
最简单的解决方案是添加
cell.font = [UIFont systemFontOfSize: 14];
后
cell.text = (NSString *)[[[...
cell.accessoryType = ...
在第一分支。
请记住,表单元是回收的。 假设您的表格有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];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.