gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:28:21 24 4
gpt4 key购买 nike

这里我有显示用户名列表的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com