UITableView 滚动时滞后 [英] UITableView lags while scrolling

查看:66
本文介绍了UITableView 滚动时滞后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题:当我滚动它时,我的 UITableViewController 滞后.它有大约 60 个自定义单元格.但是,在模拟器上它可以毫无延迟地工作.这是我的代码:CustomCell.m

here is my problem: My UITableViewController lags when I scroll it. It has about 60 custom cells. However, on the simulator it works without any lags. Here is my code: CustomCell.m

#import "CustomCell.h"

@implementation CustomCell

@synthesize nameLabel,costLabel,giftPicture;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code


        nameLabel = [[UILabel alloc]init];
        nameLabel.textAlignment = UITextAlignmentLeft;
        nameLabel.textColor = [UIColor colorWithRed:25/256.0 green:98/256.0       blue:52/256.0 alpha:1.0];
        costLabel = [[UILabel alloc]init];
        costLabel.textAlignment = UITextAlignmentLeft;


        giftPicture = [[UIImageView alloc]init];

        [self.contentView addSubview:nameLabel];
        [self.contentView addSubview:costLabel];
        [self.contentView addSubview:giftPicture];

        costLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background4"]];
        nameLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background4"]];
    }
    return self;
}
- (void)layoutSubviews {
    [super layoutSubviews];

    CGRect contentRect = self.contentView.bounds;
    CGFloat boundsX = contentRect.origin.x;
    CGRect frame;
    frame= CGRectMake(boundsX+10,10, 50, 50);
    giftPicture.frame = frame;

    frame= CGRectMake(boundsX+70 ,10, 350, 25);
    nameLabel.frame = frame;

    frame= CGRectMake(boundsX+70 ,40, 350, 18);
    costLabel.frame = frame;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

List.m

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [array1 count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[CustomCell alloc] initWithFrame:CGRectZero];
    }

    // Configure the cell...


    NSString *cellValue = [array1 objectAtIndex:indexPath.row];
    //cell.textLabel.text = cellValue; // загаловок клетки
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; // сделал чтобы клетка была со стрелочкой

    cell.nameLabel.text = cellValue;

    NSDictionary  *mainDictionary = [[NSDictionary alloc]init];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
    NSString *currentLanguage = [languages objectAtIndex:0];
    if([currentLanguage isEqualToString:@"en"])
    {
        mainDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"animals" ofType:@"txt"]];
    }
    NSEnumerator *enumerator = [mainDictionary objectEnumerator];
    id returnValue;

    while ((returnValue = [enumerator nextObject]))
    {

        if([cell.nameLabel.text isEqualToString:[returnValue valueForKey:@"name"]] )
        {

            cell.animalPicture.image = [UIImage imageNamed:[returnValue valueForKey:@"icon"]];
            cell.costLabel.text = [NSString stringWithFormat:@"$%@ - $%@",[returnValue valueForKey:@"minAge"],[returnValue valueForKey:@"maxAge"]];

        }
    }
    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 75;
}
@end

出于好奇,我尝试将自定义单元格设为空(删除标签和图像),但没有任何改变.你能告诉我,我应该检查什么来解决这些滞后吗?谢谢!

I tried to make custom cells empty (remove labels and images), just because of curious, but it didn't change anything. Could you tell me please, what I should check to fix these lags? Thanks!

推荐答案

是的,我认为这只是为每个表格单元格要做的大量工作(也永远永远不要相信模拟器的性能,这与设备上的行为如何).你应该尝试使用工具来准确地查明什么需要时间,但如果我不得不看,我会说:

Yeah thats just a TON of work to be doing for each table cell I think (also never never never trust the simulator for performance, it means nothing in relation to how things will behave on the device). You should try and use instruments to pinpoint exactly whats taking time but if I had to look at that I would say:

NSDictionary  *mainDictionary = [[NSDictionary alloc]init];

对于初学者来说,a) 似乎没有意义,因为下一行 b) 看起来你正在泄漏这个.

Just for starters this a) seems pointless because of this next line and b) looks like you are leaking this.

    mainDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"animals" ofType:@"txt"]];

每次都加载这个文件对于性能来说不是很好,你应该把这些数据带入一次以喜欢你的视图控制器,而不是每次你填写一个单元格.

Loading this file in every time cannot be stellar for performance, you should bring this data in once to like your view controller instead of every time you fill out a cell.

NSEnumerator *enumerator = [mainDictionary objectEnumerator];
id returnValue;

while ((returnValue = [enumerator nextObject]))
{

    if([cell.nameLabel.text isEqualToString:[returnValue valueForKey:@"name"]] )
    {

这里的整块,我会返工.坦率地说,使用 NSDictionary 是错误的.字典包含一个键和一个值,你不应该自己用枚举器扫描整个事物来通过字符串比较来找到你想要的值,你应该使用像

This whole chunk here, I would rework. This is to be blunt, using an NSDictionary wrong. A dictionary contains a Key and a Value, you shouldn't have to scan through the whole thing yourself with an enumerator to find the value you want by doing string comparisons, you should be using something like

[mainDictonary objectForKey:@"thingYouWant"];

按照你说的做,就像字典和字符串比较的 60 个完整枚举对表性能没有好处,也没有必要.

doing what you said is like 60 complete enumerations of the dictionary and string comparisons can't be good for table performance and isn't necessary.

这将是我首先检查性能然后根据需要返工的工作,我敢打赌您的性能问题包含在这些块中.

That would be the stuff I work check for performance first and then rework as needed, I'm willing to bet your performance issues are contained in those chunks.

这篇关于UITableView 滚动时滞后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆