ios - UITableViewCell 中的 UIButton 使表格滚动不流畅

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

最佳答案

您有一个在此方法中发出 HTTP 同步请求的查询,这几乎可以肯定是导致它变慢的原因,而不是按钮。

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

作为附加说明,每次调用此方法时,您都会将 TouchUp 监听器添加到该单元格。由于单元格被重复使用,这意味着监听器将被添加多次。

关于ios - UITableViewCell 中的 UIButton 使表格滚动不流畅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31006992/

相关文章:

ios - 从 Swift 4 中的核心数据对 TableView 中的数据进行分组

ios - 如何在 iOS Swift 中将 UIButton 操作从 View Controller 调用到另一个 View Controller ?

ios - 在后台处理位置数据

iphone - 如何在 iPhone 中制作无限 ScrollView ?

ios - cellForRowAtIndexPath 未被调用但 numbersOfRowInSection 是

ios - 如何在 ios7 的 uitableview 单元格中设置背景图像?

ios - Sprite 套件,从内存中移除动画纹理

ios - 将图像分享到 Whatsapp 和 Facebook

iphone 错误 : expected '=' , ','、 ';'、 'asm' 或 '__attribute__' foo 之前的 ' '

ios - indexpath.row 显示的行数较少