Fle*_*lea 4 iphone objective-c uitableview ios
我在使用某些子视图时遇到了性能问题UITableViewCells.在我继续滚动之后,它最终开始变得很慢.
我正在做的第一步是UIView为每个细胞创建一个共同点,基本上这是创建一个白色单元格,在带有阴影的单元格上具有圆角效果.对此的表现似乎是正常的,所以我不认为这是罪魁祸首.

这是我用来执行此操作的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *NewsCellIdentifer = @"NewsCellIdentifier";
NewsItem *item = [self.newsArray objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NewsCellIdentifer];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NewsCellIdentifer];
cell.contentView.backgroundColor = [UIColor clearColor];
UIView *whiteRoundedCornerView = [[UIView alloc] initWithFrame:CGRectMake(10,10,300,100)];
whiteRoundedCornerView.backgroundColor = [UIColor whiteColor];
whiteRoundedCornerView.layer.masksToBounds = NO;
whiteRoundedCornerView.layer.cornerRadius = 3.0;
whiteRoundedCornerView.layer.shadowOffset = CGSizeMake(-1, 1);
whiteRoundedCornerView.layer.shadowOpacity = 0.5;
[cell.contentView addSubview:whiteRoundedCornerView];
[cell.contentView sendSubviewToBack:whiteRoundedCornerView];
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
cell.layer.opaque = YES;
cell.opaque = YES;
}
[cell.contentView addSubview:[self NewsItemThumbnailView:item]];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
这是返回图形和文本的缩略图视图的方法:
- (UIView *) NewsItemThumbnailView:(NewsItem *)item
{
UIView *thumbNailMainView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 50, 70)];
UIImageView *thumbNail = [[UIImageView alloc] initWithImage:[UIImage imageNamed:item.ThumbNailFileName]];
thumbNail.frame = CGRectMake(10,10, 45, 45);
UILabel *date = [[UILabel alloc] init];
date.frame = CGRectMake(10, 53, 45, 12);
date.text = item.ShortDateString;
date.textAlignment = NSTextAlignmentCenter;
date.textColor = [BVColors WebDarkGrey];
CGFloat fontSize = 10.0;
date.font = [BVFont Museo:&fontSize];
date.opaque = YES;
thumbNail.opaque = YES;
thumbNailMainView.opaque = YES;
[thumbNailMainView addSubview:thumbNail];
[thumbNailMainView addSubview:date];
return thumbNailMainView;
}
Run Code Online (Sandbox Code Playgroud)
性能问题似乎是当我将缩略图视图添加到单元格时,因为当我评论该行时,我似乎没有它.缩略图信息是动态的,并将随每个单元格而变化.我将不胜感激任何关于如何在不降低性能的情况下做到这一点的建议.
UITableViewtableView:cellForRowAtIndexPath:每次进入视图时都会调用,并且dequeueReusableCellWithIdentifier:如果可用,将重用现有的单元对象.这两个事实相结合,使您进入一个场景,每次滚动时,相同数量的单元格对象最终会有越来越多的子视图.
正确的方法是创建一个UITableViewCell具有属性的自定义子类thumbnailView.在该属性的setter中,删除以前的缩略图(如果有),然后将新的缩略图添加到contentView.这可确保您在任何时候都只有一个缩略图子视图.
一种不太理想的方法是向UIView返回的from NewsItemThumbnailView(thumbNailMainView.tag = someIntegerConstant)添加一个标记,然后搜索带有该标记的任何视图,并在添加另一个之前将其删除:
// remove old view
UIView *oldThumbnailView = [cell.contentView viewWithTag:someIntegerConstant];
[oldThumbnailView removeFromSuperview];
// add new view
[cell.contentView addSubview:[self NewsItemThumbnailView:item]];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2265 次 |
| 最近记录: |