ios - UITableview 项目向上滚动后变为粗体

标签 ios uitableview label uilabel tableview

好吧,也许我太挑剔了。如果您看不到它,请提前道歉,但在第二张图片中,该行上的项目有点像素化,或者好像一个词被放在了一个词的顶部。这只发生在我向上滚动表格 View 之后,否则,它类似于第一张图像。

第一张图:

http://oi61.tinypic.com/308grjc.jpg

第二张图片(你可以在这里看到字体的不同):http://oi61.tinypic.com/2mqpbb9.jpg

一旦我向上滚动,它就会更加大胆和粗俗。

坦率地说,我不知道为什么。 TableView 正在正常加载。任何帮助、预感或建议都会有很大的关联,即使它可以将我指向正确的区域。

这是第二张图片的表格 View 的代码。

static NSString *CellIdentifier = @"OrderProductCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1    reuseIdentifier:CellIdentifier] autorelease];

}

//cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];


//configure cell, display name and cost of each item
OrderProduct *p = [[order.items allObjects] objectAtIndex:indexPath.row];

UILabel *lableName=[[UILabel alloc]initWithFrame:CGRectMake(10, 16, 300, 20)];
lableName.backgroundColor=[UIColor clearColor];
lableName.font=[UIFont boldSystemFontOfSize:19];
[lableName setFont:[UIFont fontWithName:@"Arial-Bold" size:12.0]];
lableName.text=p.name;

//UILabel *counterNumber=[[UILabel alloc]initWithFrame:CGRectMake(10, 25, 300, 20)];
//counterNumber.backgroundColor=[UIColor clearColor];
//counterNumber.font=[UIFont systemFontOfSize:12];
//counterNumber.text=[NSString stringWithFormat:@"Scan time :%@",p.scannerCounter];
[cell.detailTextLabel setText:[NSString stringWithFormat:@"%@ %.02f",p.currency, p.cost]];
cell.imageView.image = [UIImage imageNamed:p.image];
[cell addSubview:lableName];
//[cell release];
//[cell addSubview:counterNumber];
return cell;

最佳答案

发生这种情况是因为您没有重复使用单元格,因此您基本上是一次又一次地添加标签,而不是使用已经存在的标签。

只需将以下代码放入 if(cell == nil) 中:

static NSString *CellIdentifier = @"OrderProductCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *lableName;
if(cell==nil){  
    // this is where we should initialize and customize the UI elements,
    // or in this case your label.
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    lableName=[[UILabel alloc]initWithFrame:CGRectMake(10, 16, 300, 20)];
    lableName.backgroundColor=[UIColor clearColor];
    lableName.font=[UIFont boldSystemFontOfSize:19];
    [lableName setFont:[UIFont fontWithName:@"Arial-Bold" size:12.0]];
    [cell addSubview:lableName];
}
// this is where we should assign the value.
OrderProduct *p = [[order.items allObjects] objectAtIndex:indexPath.row];    
lableName.text = p.name;
/*
assign other values if any
...
*/
return cell;

这样,标签名称就不会一次又一次地添加到单元格中,因此不会发生此问题。

重用单元格属性的最佳方法是初始化和自定义 if(cell==nil) 中的所有内容,并且仅在 if 条件之外添加元素的值。

关于ios - UITableview 项目向上滚动后变为粗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21472602/

相关文章:

ios - 为什么 scrollViewWillEndDragging 会影响 UICollectionView 和 UITableView?

java - 如何更改JUNG中边的边标签?

ios - dyld : Library not loaded: @rpath/libswiftCore. dylib 问题仅在模拟器上持续存在

iphone - 控制其他应用程序的音量

ios - cellForRowAtIndex 自定义方法

swift - 标签如何在多行中显示字符串

Java - 添加标签/删除按钮

ios - 音频队列播放结束的准确时间

ios - UIView 动画使 UICollectionView 委托(delegate)变得模糊 - Swift

ios - 标题部分 View UITableView 中的标签和按钮