• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 使用可重用单元格在 iPhone 上缓慢加载 tableview

[复制链接]
菜鸟教程小白 发表于 2022-12-13 14:47:37 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我从 Parse.com 后端加载数据,他们向我发送了使用可重用单元格的解决方案,但现在我仍然遇到加载速度问题,这是我在 tableview 中的编码,我有一个用于制作的子类我的单元格 (ExploreStreamCustomCell.m)

    - (ExploreStreamCustomCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath 
                        objectPFObject *)object 
{
    static NSString *CellIdentifier = @"ExploreStreamCustomCell";

    ExploreStreamCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[ExploreStreamCustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                              reuseIdentifier:CellIdentifier];
    }

    // Configure the cell
    cell.listItemTitle.text = [object objectForKey"text"];
    cell.checkinsLabel.text = [NSString stringWithFormat"%@", [object objectForKey"checkins"]];
    cell.descriptionLabel.text = [object objectForKey"description"];


    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    PFFile *listThumbnail = [object objectForKey"header"];
    cell.listViewImage.image = [UIImage imageNamed"loading_image_stream.png"]; // placeholder image
    cell.listViewImage.file = listThumbnail;
    [cell.listViewImage loadInBackground:NULL];

    return cell;
}

- (UITableViewCell *)tableViewUITableView *)tableView cellForNextPageAtIndexPathNSIndexPath *)indexPath {
    UITableViewCell *cell = [super tableView:tableView cellForNextPageAtIndexPath:indexPath];
    cell.textLabel.font = [cell.textLabel.font fontWithSize:kPAWWallPostTableViewFontSize];
    return cell;
}

如果我在单元格 == nil 中有//configure 单元格的所有内容,它会很快,但它会显示 9 个唯一数据行中的 3 个,并重复这 3 个唯一内容单元格 3 次?

在 ExploreStreamCustomCell.m 中编辑额外代码

#import "ExploreStreamCustomCell.h"

@implementation ExploreStreamCustomCell

@synthesize listViewImage, 
iconLocation, 
iconPeople, 
iconCheckins, 
listItemTitle, 
locationLabel, 
peopleLabel, 
checkinsLabel, 
descriptionLabel,
listItemView;


- (id)initWithStyleUITableViewCellStyle)style reuseIdentifierNSString *)reuseIdentifier {
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        //Initialization code
        listItemView = [[UIView alloc] init];
        listViewImage = [[PFImageView alloc] init];
        iconLocation = [[UIImageView alloc] init];
        iconPeople = [[UIImageView alloc] init];
        iconCheckins = [[UIImageView alloc] init];
        listItemTitle = [[UILabel alloc] init];     
        locationLabel = [[UILabel alloc] init]; 
        peopleLabel = [[UILabel alloc] init];
        checkinsLabel = [[UILabel alloc] init]; 
        descriptionLabel = [[UILabel alloc] init];

        listViewImage.image = [UIImage imageNamed"nachtwacht_list_formaat.png"];
        iconLocation.image = [UIImage imageNamed"icon_magenta_location.png"];
        iconPeople.image = [UIImage imageNamed"icon_magenta_people.png"];
        iconCheckins.image = [UIImage imageNamed"icon_magenta_checkins.png"];
        listItemTitle.text = @"text";
        locationLabel.text = @"0,7 km";
        peopleLabel.text = @"34";
        checkinsLabel.text = @"61";
        descriptionLabel.text = @"Description text.";

        [self.contentView addSubview:listItemView];
        [self.contentView addSubview:listViewImage];
        [self.contentView addSubview:iconLocation];
        [self.contentView addSubview:iconPeople];
        [self.contentView addSubview:iconCheckins];
        [self.contentView addSubview:listItemTitle];
        [self.contentView addSubview:locationLabel];
        [self.contentView addSubview:peopleLabel];
        [self.contentView addSubview:checkinsLabel];
        [self.contentView addSubview:descriptionLabel];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGRect contentRect = self.contentView.bounds;
    CGFloat boundsX = contentRect.origin.x;
    CGRect frame;

    frame= CGRectMake(boundsX+0 , 33, 280, 124);
    listViewImage.frame = frame;
    listViewImage.contentMode = UIViewContentModeScaleAspectFill;
    listViewImage.layer.masksToBounds = YES;

    //listViewImage.backgroundColor = [UIColor lightGrayColor];

    frame= CGRectMake(boundsX+20 , 164, 12, 18);
    iconLocation.frame = frame;
    //iconLocation.backgroundColor = [UIColor lightGrayColor];

    frame= CGRectMake(boundsX+102 , 164, 24, 18);
    iconPeople.frame = frame;
    //iconPeople.backgroundColor = [UIColor lightGrayColor];

    frame= CGRectMake(boundsX+193 , 164, 20, 16);
    iconCheckins.frame = frame;
    //iconLocation.backgroundColor = [UIColor lightGrayColor];

    frame= CGRectMake(boundsX+0 , 0, 280, 33);
    listItemView.frame = frame;
    listItemView.backgroundColor = [UIColor colorWithRed:0.749 green:0.000 blue:0.243 alpha:1.000];


    frame= CGRectMake(boundsX+20 , 3, 240, 29);
    listItemTitle.frame = frame;
    //listItemTitle.textColor = [UIColor colorWithRed:250.0f/255.0f green:194.0f/255.0f blue:9.0f/255.0f alpha:0.8f];
    listItemTitle.textAlignment = UITextAlignmentLeft;
    listItemTitle.font = [UIFont boldSystemFontOfSize:15];
    listItemTitle.textColor = [UIColor whiteColor];
    listItemTitle.backgroundColor = [UIColor clearColor];
    listItemTitle.lineBreakMode = UILineBreakModeTailTruncation;
    //listItemTitle.backgroundColor = [UIColor orangeColor];

    frame= CGRectMake(boundsX+40 , 164, 57, 21);
    locationLabel.frame = frame;
    locationLabel.textAlignment = UITextAlignmentLeft;
    locationLabel.font = [UIFont boldSystemFontOfSize:12];
    locationLabel.textColor = [UIColor colorWithRed:0.749 green:0.000 blue:0.243 alpha:1.000];
    locationLabel.backgroundColor = [UIColor clearColor];
    locationLabel.lineBreakMode = UILineBreakModeTailTruncation;
    locationLabel.numberOfLines = 1;
    //locationLabel.backgroundColor = [UIColor redColor];

    frame= CGRectMake(boundsX+134 , 164, 57, 21);
    peopleLabel.frame = frame;
    peopleLabel.textAlignment = UITextAlignmentLeft;
    peopleLabel.font = [UIFont boldSystemFontOfSize:12];
    peopleLabel.textColor = [UIColor colorWithRed:0.749 green:0.000 blue:0.243 alpha:1.000]; 
    peopleLabel.backgroundColor = [UIColor clearColor];
    peopleLabel.lineBreakMode = UILineBreakModeTailTruncation;
    peopleLabel.numberOfLines = 1;

    frame= CGRectMake(boundsX+221 , 164, 51, 21);
    checkinsLabel.frame = frame;
    checkinsLabel.textAlignment = UITextAlignmentLeft;
    checkinsLabel.font = [UIFont boldSystemFontOfSize:12];
    checkinsLabel.textColor = [UIColor colorWithRed:0.749 green:0.000 blue:0.243 alpha:1.000]; 
    checkinsLabel.backgroundColor = [UIColor clearColor];
    checkinsLabel.lineBreakMode = UILineBreakModeTailTruncation;
    checkinsLabel.numberOfLines = 1;

    frame= CGRectMake(boundsX+0 , 189, 280, 55);
    descriptionLabel.frame = frame;
    descriptionLabel.textAlignment = UITextAlignmentLeft;
    descriptionLabel.font = [UIFont systemFontOfSize:13];
    descriptionLabel.backgroundColor = [UIColor clearColor];
    descriptionLabel.lineBreakMode = UILineBreakModeTailTruncation;
    descriptionLabel.numberOfLines = 3;
}

- (void)setSelectedBOOL)selected animatedBOOL)animated {

    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


/*
- (id)initWithFrameCGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end



Best Answer-推荐答案


探索代码效率低下的一个好方法是使用 Instruments 的 Time Profiler 工具。 Time Profiler 可以让您在代码中逐行查看每个任务花费了多少时间。

Time Profiler


我会推荐以下配置设置:

Profiler settings


来自 Apple 的人脸检测示例应用:

Face Detection


然后您可以双击任何一行(百分比越高意味着该方法调用所花费的时间越多),以在代码中查看每个地方花费了多少时间。

Code analysis


从这里您可以开始找出您效率低下的地方,并准确了解是什么占用了这么多时间。祝你好运!

关于ios - 使用可重用单元格在 iPhone 上缓慢加载 tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11494914/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap