SanitLee
SanitLee

Reputation: 1253

UIButton in UITableViewCell makes table scrolling not smooth

Here I have UITableView that displays username list and it works just fine. However, after I added UIButton into each cell for each username in order to follow/unfollow them, that makes the table scrolling very not smooth. Here below is my relevant code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *identifier = @"reuseIdentifier";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifier];

    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                  reuseIdentifier:identifier];
    }


    //*** Display username ***
    NSString *userName = [NSString stringWithFormat:@"@%@", [self.objects[indexPath.row] objectForKey:kPAWParseUsernameKey]];
    cell.textLabel.text = userName;
    cell.textLabel.textColor = [UIColor darkGrayColor];


    //*** Follow/Unfollow button -- This makes the table scrolling very not smooth ***
    UIButton *followButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cell.contentView addSubview:followButton];
    PFQuery *query = [PFUser query];
    [query whereKey:kPAWParseUsernameKey equalTo:[self.objects[indexPath.row] objectForKey:kPAWParseUsernameKey]];
    NSArray *userArray = [query findObjects];
    PFUser *user = [userArray objectAtIndex:0];
    // set follow button image
    if ([[[PFUser currentUser] objectForKey:@"followings"]containsObject:user.objectId]) {
        [followButton setImage:[UIImage imageNamed:@"following.png"] forState:UIControlStateNormal];    
    } else {
        [followButton setImage:[UIImage imageNamed:@"unfollow.png"] forState:UIControlStateNormal];
    }
    [followButton setFrame:CGRectMake(280.0f, 20.0f, 20.0f, 32.0f)];
    [followButton addTarget:self action:@selector(didTapFollowButtonAction:) forControlEvents:UIControlEventTouchUpInside];


    return cell;

}

How can I properly add UIButton so that it won't cause table scrolling issue? Thanks in advance.

Solution: Following an answer by shortstuffsushi, use findObjectsInBackgroundWithBlock instead of findObjects for the UIButton.

[query findObjectsInBackgroundWithBlock:^(NSArray *userArray, NSError *error) {
    if(!error){
        PFUser *user = [userArray objectAtIndex:0];
        if ([[[PFUser currentUser] objectForKey:@"followings"]containsObject:user.objectId]) {
            [followButton setImage:[UIImage imageNamed:@"following.png"] forState:UIControlStateNormal];
        } else {
            [followButton setImage:[UIImage imageNamed:@"unfollow.png"] forState:UIControlStateNormal];
        }
        [followButton setFrame:CGRectMake(280.0f, 20.0f, 20.0f, 32.0f)];
        [followButton addTarget:self action:@selector(didTapFollowButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    } else {
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
}];

Upvotes: 1

Views: 186

Answers (1)

shortstuffsushi
shortstuffsushi

Reputation: 2330

You have a query that makes an HTTP synchronous request within this method, that's almost certainly what's slowing it down, not the button.

PFQuery *query = [PFUser query]; [query whereKey:kPAWParseUsernameKey equalTo:[self.objects[indexPath.row] objectForKey:kPAWParseUsernameKey]]; NSArray *userArray = [query findObjects];

As an additional note, you're adding the TouchUp listener to that cell each time this method is hit. Since cells are reused, that means the listener will be added multiple times.

Upvotes: 2

Related Questions