使UITableView更顺畅

时间:2015-04-13 03:39:06

标签: objective-c xcode uitableview tableview

我有一个显示图片和文字的UITableView。它是一个社交应用程序,所以我必须从数据库中检索所有数据,我正在使用Parse。继承人的代码。我知道它很长,但如果你可以掠过它那将是伟大的。我有三个不同的自定义单元格(笔尖)将根据内容加载。一个cell - 只是一个字符串帖子,groupPost - 也是一个字符串帖子,一个imageCell - 用于图片帖子。我很困惑如何制作更流畅的滚动,如facebooks或instagrams。我应该如何加载图像,并检索数据?我查看了帖子并看到了缓存它的建议,但不确定如何做到这一点。谢谢你的帮助

     postCell *cell = (postCell *)[tableView dequeueReusableCellWithIdentifier:@"postCell"];
groupPostCell *groupPost = (groupPostCell *) [self.tableView dequeueReusableCellWithIdentifier:@"groupPostCell"];
pictureCell *imageCell = (pictureCell *) [self.tableView dequeueReusableCellWithIdentifier:@"imageCell"];
PFObject *object = [self.friendPosts objectAtIndex:indexPath.row];

if ([object objectForKey:@"Image"] != nil) {
    if (imageCell == nil) {
        imageCell = [[pictureCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"imageCell"];
    }

    PFFile *file = [object objectForKey:@"Image"];
    NSData *data = [file getData];
    imageCell.imagePost.image = [UIImage imageWithData:data];
    imageCell.caption.text = [object objectForKey:@"stringPost"];
    PFFile *profileFile = [object objectForKey:@"profileImage"];
    NSData *profileData = [profileFile getData];
    imageCell.profileImage.image = [UIImage imageWithData:profileData];

    [imageCell.name setTitle:[object objectForKey:@"User_Name"] forState:UIControlStateNormal];
    [imageCell.eventButton setTitle:[object objectForKey:@"Event"] forState:UIControlStateNormal];
    imageCell.name.hidden = false;
    imageCell.eventButton.hidden = false;

    imageCell.name.tag = indexPath.row;
    imageCell.profileImageButton.tag = indexPath.row;
    imageCell.eventButton.tag = indexPath.row;

    [imageCell.name addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [imageCell.profileImageButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [imageCell.eventButton addTarget:self action:@selector(eventPage:) forControlEvents:UIControlEventTouchUpInside];

    NSArray *likesArray = [object objectForKey:@"likes"];
    NSString *likeString = [NSString stringWithFormat:@"%lu Likes", (unsigned long)likesArray.count];
    if ([likesArray containsObject:[PFUser currentUser].objectId]) {
        imageCell.rateButton.hidden = true;
    }
    else{
        imageCell.rateButton.hidden = false;
    }

    NSArray *commentArray = [object objectForKey:@"Comments"];
    NSString *commentString = [NSString stringWithFormat:@"%lu Comments", (unsigned long)commentArray.count];

    [imageCell.likesButton setTitle:likeString forState:UIControlStateNormal];
    [imageCell.commentsButton setTitle:commentString forState:UIControlStateNormal];

    imageCell.rateButton.tag = indexPath.row;
    imageCell.likesButton.tag = indexPath.row;
    imageCell.commentsButton.tag = indexPath.row;
    imageCell.flagButton.tag = indexPath.row;

    [imageCell.rateButton addTarget:self action:@selector(like:) forControlEvents:UIControlEventTouchUpInside];
    [imageCell.likesButton addTarget:self action:@selector(likesPage:) forControlEvents:UIControlEventTouchUpInside];
    [imageCell.commentsButton addTarget:self action:@selector(imageComment:) forControlEvents:UIControlEventTouchUpInside];
    [imageCell.flagButton addTarget:self action:@selector(flagPost:) forControlEvents:UIControlEventTouchUpInside];

    [imageCell.timeButton setTitle:[self getDaysBetween:object.createdAt] forState:UIControlStateNormal];

    return imageCell;
}

if ([object objectForKey:@"Group"] == nil) {

    if (cell == nil) {
        cell = [[postCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"postCell"];
    }

    cell.personStringPost.text = [object objectForKey:@"stringPost"];
    if ([object objectForKey:@"Event"] == nil) {
        [cell.noEventNameButton setTitle:[object objectForKey:@"User_Name"] forState:UIControlStateNormal];
        cell.noEventNameButton.tag = indexPath.row;
        [cell.noEventNameButton addTarget:self action:@selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];
        cell.nameLabel.hidden = true;
        cell.eventLabel.hidden = true;
        cell.noEventNameButton.hidden = false;
    }
    else{
        [cell.nameLabel setTitle:[object objectForKey:@"User_Name"] forState:UIControlStateNormal];
        cell.nameLabel.tag = indexPath.row;
        [cell.nameLabel addTarget:self action:@selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];
        cell.noEventNameButton.hidden = true;
        cell.nameLabel.hidden = false;
        cell.eventLabel.hidden = false;
        [cell.eventLabel setTitle:[object objectForKey:@"Event"] forState:UIControlStateNormal];
        [cell.eventLabel addTarget:self action:@selector(eventPage:) forControlEvents:UIControlEventTouchUpInside];

    }

    [cell.timeLabel setTitle:[self getDaysBetween:object.createdAt] forState:UIControlStateNormal];

    PFFile *imageFile = [object objectForKey:@"profileImage"];
    NSData *data = [imageFile getData];
    cell.profileImage.image = [UIImage imageWithData:data];

    NSArray *likesArray = [object objectForKey:@"likes"];
    NSString *likeString = [NSString stringWithFormat:@"%lu Likes", (unsigned long)likesArray.count];
    if ([likesArray containsObject:[PFUser currentUser].objectId]) {
        cell.likeButton.hidden = true;
    }
    else{
        cell.likeButton.hidden = false;
    }

    NSArray *commentArray = [object objectForKey:@"Comments"];
    NSString *commentString = [NSString stringWithFormat:@"%lu Comments", (unsigned long)commentArray.count];

    [cell.likePage setTitle:likeString forState:UIControlStateNormal];
    [cell.commentButton setTitle:commentString forState:UIControlStateNormal];

    cell.eventLabel.tag = indexPath.row;
    cell.profileImageButton.tag = indexPath.row;
    cell.likeButton.tag = indexPath.row;
    cell.commentButton.tag = indexPath.row;
    cell.likePage.tag = indexPath.row;
    cell.flagButton.tag = indexPath.row;

    [cell.flagButton addTarget:self action:@selector(flagPost:) forControlEvents:UIControlEventTouchUpInside];
    [cell.profileImageButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [cell.likePage addTarget:self action:@selector(likesPage:) forControlEvents:UIControlEventTouchUpInside];
    [cell.likeButton addTarget:self action:@selector(like:) forControlEvents:UIControlEventTouchUpInside];
    [cell.commentButton addTarget:self action:@selector(commentsPage:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

if ([object objectForKey:@"Group"] != nil) {

    if (groupPost == nil) {
        groupPost = [[groupPostCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"groupPostCell"];
    }

    [groupPost.groupNameButton setTitle:[object objectForKey:@"Group"] forState:UIControlStateNormal];
    groupPost.groupNameButton.tag = indexPath.row;
    [groupPost.groupNameButton addTarget:self action:@selector(groupPage:) forControlEvents:UIControlEventTouchUpInside];
    [groupPost.groupNameButton setTitleColor:[self checkType:[object objectForKey:@"Type"]] forState:UIControlStateNormal];

    NSArray *likesArray = [object objectForKey:@"likes"];
    NSString *likeString = [NSString stringWithFormat:@"%lu Likes", (unsigned long)likesArray.count];
    if ([likesArray containsObject:[PFUser currentUser].objectId]) {
        groupPost.likeButton.hidden = true;
    }
    else{
        groupPost.likeButton.hidden = false;
    }
    NSArray *commentArray = [object objectForKey:@"Comments"];
    NSString *commentString = [NSString stringWithFormat:@"%lu Comments", (unsigned long)commentArray.count];

    [groupPost.likePage setTitle:likeString forState:UIControlStateNormal];
    [groupPost.commentsButton setTitle:commentString forState:UIControlStateNormal];

    groupPost.likePage.tag = indexPath.row;
    groupPost.likeButton.tag = indexPath.row;
    groupPost.commentsButton.tag = indexPath.row;

    [groupPost.likeButton addTarget:self action:@selector(like:) forControlEvents:UIControlEventTouchUpInside];
    [groupPost.likePage addTarget:self action:@selector(likesPage:) forControlEvents:UIControlEventTouchUpInside];
    [groupPost.commentsButton addTarget:self action:@selector(commentsPage:) forControlEvents:UIControlEventTouchUpInside];

    if ([object objectForKey:@"Event"] == nil) {
        groupPost.eventButton.hidden = true;
    }
    else{
        groupPost.eventButton.hidden = false;
        [groupPost.eventButton setTitle:[object objectForKey:@"Event"] forState:UIControlStateNormal];
        groupPost.eventButton.tag = indexPath.row;
        [groupPost.eventButton addTarget:self action:@selector(eventPage:) forControlEvents:UIControlEventTouchUpInside];
    }

    groupPost.flagButton.tag = indexPath.row;
    [groupPost.flagButton addTarget:self action:@selector(flagPost:) forControlEvents:UIControlEventTouchUpInside];

    [groupPost.timeButton setTitle:[self getDaysBetween:object.createdAt] forState:UIControlStateNormal];
    groupPost.postLabel.text = [object objectForKey:@"stringPost"];
    return groupPost;
}

1 个答案:

答案 0 :(得分:0)

每当您处理服务器上的数据时,请尽量避免不惜一切代价同步加载数据。该行

 NSData *data = [imageFile getData];[imageFile getData];

将使得在完成下载数据之前,其他任何东西都无法在主线程上执行。这是你应该怎么做的:

[imageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
    if (!error) {
        cell.profileImage.image = [UIImage imageWithData:imageData];
    }
}];

这将使得所有其他代码在主线程上执行,然后当数据完成下载时,它将加载到图像中。我建议在“cell.profileImage”中放置占位符或加载视图,然后当从Parse加载图像时,它将覆盖该占位符。