在向一些路径添加字体和阴影后,我注意到当视图从堆栈中弹出时,表视图动画会滞后(像FB/ UILabels使用的侧扫)。侧扫是平滑的,直到我添加了UILabel阴影。
我想我可能把它添加到了错误的位置,所以标签属性可能被错误地添加了。请看下面的cellForRowAtIndexPath:方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellReuseIdentifier = @"cellReuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier];
}
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 2, self.view.bounds.size.width, 200)];
imageView.image = [UIImage imageNamed:@"rest.jpg"];
[cell.contentView addSubview:imageView];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, 320, 30)];
titleLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"title"];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor whiteColor];
[titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:24]];
titleLabel.layer.shadowColor = [[UIColor whiteColor] CGColor];
titleLabel.layer.shadowOpacity = 0.7;
[cell.contentView addSubview:titleLabel];
UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, cell.bounds.size.width, 30)];
detailLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"description"];
detailLabel.backgroundColor = [UIColor clearColor];
detailLabel.textColor = [UIColor whiteColor];
[detailLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:18]];
detailLabel.layer.shadowColor = [[UIColor whiteColor] CGColor];
detailLabel.layer.shadowOpacity = 0.7;
[cell.contentView addSubview:detailLabel];
cell.contentView.backgroundColor = [UIColor clearColor];
return cell;
}谢谢你的帮助。
发布于 2013-05-30 18:30:20
您总是在添加新的子视图。因此,每当您滚动表格视图时,单元格中添加的内容就会越来越多。
在创建单元格时创建所有子视图,然后只更新子视图设置。类似于:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellReuseIdentifier = @"cellReuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 2, self.view.bounds.size.width, 200)];
imageView.tag = 123123;
[cell.contentView addSubview:imageView];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, 320, 30)];
titleLabel.tag = 234234];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor whiteColor];
[titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:24]];
titleLabel.layer.shadowColor = [[UIColor whiteColor] CGColor];
titleLabel.layer.shadowOpacity = 0.7;
[cell.contentView addSubview:titleLabel];
UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, cell.bounds.size.width, 30)];
detailLabel.tag = 345345];
detailLabel.backgroundColor = [UIColor clearColor];
detailLabel.textColor = [UIColor whiteColor];
[detailLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:18]];
detailLabel.layer.shadowColor = [[UIColor whiteColor] CGColor];
detailLabel.layer.shadowOpacity = 0.7;
[cell.contentView addSubview:detailLabel];
cell.contentView.backgroundColor = [UIColor clearColor];
}
UIImageView *imageView = (UIImageView *)[cell viewWithTag:123123];
imageView.image = [UIImage imageNamed:@"rest.jpg"];
UILabel *titleLabel = (UILabel *)[cell viewWithTag:234234];
titleLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"title"];
UILabel *detailLabel = (UILabel *)[cell viewWithTag:345345];
detailLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"description"];
return cell;
} 发布于 2013-05-30 18:28:32
由于文本属性从不更改,因此将设置它们的代码移到if语句中。只将设置图像和标签文本的代码保留在if语句之外。单元格被重用,因此字体等属性将保留在单元格中,即使它被“回收”。在else分支中,添加查找单元格中现有标签的代码。否则,您将多次向单元格添加相同的标签。
发布于 2013-05-30 18:30:53
您可以添加子视图,即使在退出队列而不是初始化新单元之后也是如此。确保创建和添加子视图的所有代码仅在初始化单元格时完成。如果需要参考单元格中的视图进行配置,可以子类UITableViewCell。
此外,阴影渲染也可能会减慢它,添加一个阴影路径来使渲染更有效:
添加到您的tableView:cellForRowAtIndexPath:方法中:
...
CGPathRef shadowPath = [UIBezierPath bezierPathWithRect:detailLabel.layer.bounds].CGPath;
detailLabel.layer.shadowPath = shadowPath;
...https://stackoverflow.com/questions/16833688
复制相似问题