UITableViewCell中的UIButton使表格滚动不顺畅

时间:2015-06-23 15:25:20

标签: ios objective-c uitableview uibutton

这里我有UITableView显示用户名列表,它工作得很好。 但是,在我为每个用户名的每个单元格添加UIButton以跟踪/取消关注它们之后,这会使表格滚动非常不顺畅。以下是我的相关代码:

- (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;

}

如何正确添加UIButton以免造成桌面滚动问题?提前致谢。

解决方案: 在shortstuffsushi的回答之后,使用findObjectsInBackgroundWithBlock而不是UIButton的findObjects。

[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]);
    }
}];

1 个答案:

答案 0 :(得分:2)

你有一个查询在这个方法中发出一个HTTP同步请求,这几乎可以肯定会减慢它的速度,而不是按钮。

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

作为补充说明,每次触发此方法时,您都会将TouchUp侦听器添加到该单元格。由于单元格被重用,这意味着将多次添加侦听器。