UITableViewCell 中的 UIButton 使 table 滚动不流畅
UIButton in UITableViewCell makes table scrolling not smooth
我这里有显示用户名列表的 UITableView,它工作得很好。 但是,在我将 UIButton 添加到每个用户名的每个单元格中以便 follow/unfollow 它们之后,这使得 table 滚动非常不流畅。 以下是我的相关内容代码:
- (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 以使其不会导致 table 滚动问题? 在此先感谢。
解法:
按照 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];
[query whereKey:kPAWParseUsernameKey equalTo:[self.objects[indexPath.row] objectForKey:kPAWParseUsernameKey]];
NSArray *userArray = [query findObjects];
另外请注意,每次点击此方法时,您都会将 TouchUp 侦听器添加到该单元格。由于单元格被重复使用,这意味着监听器将被添加多次。
我这里有显示用户名列表的 UITableView,它工作得很好。 但是,在我将 UIButton 添加到每个用户名的每个单元格中以便 follow/unfollow 它们之后,这使得 table 滚动非常不流畅。 以下是我的相关内容代码:
- (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 以使其不会导致 table 滚动问题? 在此先感谢。
解法: 按照 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];
[query whereKey:kPAWParseUsernameKey equalTo:[self.objects[indexPath.row] objectForKey:kPAWParseUsernameKey]];
NSArray *userArray = [query findObjects];
另外请注意,每次点击此方法时,您都会将 TouchUp 侦听器添加到该单元格。由于单元格被重复使用,这意味着监听器将被添加多次。