在滚动之前,UITableViewCell按钮不显示选定或取消选择的状态

时间:2016-06-24 11:01:15

标签: ios objective-c uitableview parse-platform uibutton

我正在使用uitableviewcell中的“like”按钮处理应用程序,但是除非滚动uitableview并且单元格不在屏幕上并重新出现,否则单元格的状态不会显示。以下是我尝试显示按钮的方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"ProfileCell";
    PFObject *data = self.posts[indexPath.row];
    NSLog(@"Data: %@", data);

    ProfileTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil)
    {


        cell = [[ProfileTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                           reuseIdentifier:MyIdentifier];


    }

    for(UIView *view in cell.contentView.subviews){
        if ([view isKindOfClass:[UIView class]]) {
            [view removeFromSuperview];
        }
    }

#pragma mark - Heart Button
    heartButton = [[UIButton alloc] init];
    [heartButton setImage:[UIImage imageNamed:@"heartButton"]
                 forState:UIControlStateNormal];
    [heartButton setImage:[UIImage imageNamed:@"heartButtonSelected"]
                 forState:UIControlStateSelected];
    dispatch_async(dispatch_get_main_queue(), ^ {
        PFQuery *query = [PFQuery queryWithClassName:@"OrganizationLike"];
        [query whereKey:@"Post" equalTo:data];
        [query whereKey:@"liker" equalTo:[PFUser currentUser]];
        [query getFirstObjectInBackgroundWithBlock:^(PFObject * _Nullable object, NSError * _Nullable error) {
            if (!object) {
                [heartButton setSelected:NO];
            }else{
                [heartButton setSelected:YES];
            }
        }];

    });
    [heartButton addTarget:self action:@selector(heartButton:) forControlEvents:UIControlEventTouchDown];
    [heartButton setTranslatesAutoresizingMaskIntoConstraints:NO];
    [cell.contentView addSubview:heartButton];


    [heartButton autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:80];
    [heartButton autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:7.0];
    [heartButton autoSetDimension:ALDimensionHeight toSize:15];
    [heartButton autoSetDimension:ALDimensionWidth toSize:16];

    PFQuery *lquery = [PFQuery queryWithClassName:@"OrganizationLike"];
    [lquery whereKey:@"Post" equalTo:data];
    [lquery findObjectsInBackgroundWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) {
        if (objects.count>0) {
            UILabel *likeLabel = [[UILabel alloc] init];
            [likeLabel setNeedsDisplay];
            [likeLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
            likeLabel.backgroundColor = [UIColor clearColor];
            likeLabel.textColor = [UIColor colorWithRed:0.643 green:0.655 blue:0.667 alpha:1];
            likeLabel.font = [UIFont fontWithName:@"OpenSans-Light" size:8.881];
            likeLabel.textAlignment = NSTextAlignmentCenter;
            likeLabel.text = [NSString stringWithFormat:@"%d", objects.count];
            [likeLabel setNeedsDisplay];
            [cell.contentView addSubview:likeLabel];

            [likeLabel autoAlignAxis:ALAxisHorizontal toSameAxisOfView:heartButton withOffset:0];
            [likeLabel autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:heartButton withOffset:3];
        }
    }];

    return cell;
}

以下是处理按钮的方法:

- (void)heartButton:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        NSIndexPath *i=[self indexPathForCellContainingView:sender.superview];
        PFObject *data = self.posts[i.row];
        PFQuery *query = [PFQuery queryWithClassName:@"OrganizationLike"];
        [query whereKey:@"Post" equalTo:data];
        [query whereKey:@"liker" equalTo:[PFUser currentUser]];
        [query getFirstObjectInBackgroundWithBlock:^(PFObject * _Nullable object, NSError * _Nullable error) {
            if (!object) {
                PFObject *like = [PFObject objectWithClassName:@"OrganizationLike"];
                like[@"liker"] = [PFUser currentUser];
                like[@"Post"] = data;
                [like saveInBackground];
                [self.tableView reloadData];
            }
        }];
    } else {
        NSIndexPath *i=[self indexPathForCellContainingView:sender.superview];
        PFObject *data = self.posts[i.row];
        PFQuery *query = [PFQuery queryWithClassName:@"OrganizationLike"];
        [query whereKey:@"Post" equalTo:data];
        [query whereKey:@"liker" equalTo:[PFUser currentUser]];
        [query getFirstObjectInBackgroundWithBlock:^(PFObject * _Nullable object, NSError * _Nullable error) {
            [object deleteInBackground];
            [self.tableView reloadData];
        }];
    }
}

3 个答案:

答案 0 :(得分:0)

在选择或更新布局显示后重新加载表格视图

答案 1 :(得分:0)

首先尝试在主线程中调用[self.tableView reloadData]。

再次检查是否调用[self.tableView reloadData]。您可以通过添加断点进行检查。

您可以通过调用

在特定的索引路径上重新加载tableView,而不是重新加载整个tableView单元格

[self.tableView reloadRowsAtIndexPaths:indexPath withRowAnimation:nil];

答案 2 :(得分:0)

检查这可能有效,

- (void)heartButton:(UIButton *)sender

中的

首先获取更新的对象并将其替换为主数组对象 [self.posts replaceObjectAtIndex:index withObject:newObj];

比使用 [self.tableView reloadRowsAtIndexPaths:indexPath withRowAnimation:nil];

更多建议不需要首先从单元格视图中删除所有对象,而不是在cellForRowAtIndexPath:方法中创建新对象,只需检查视图对象是否已存在,而不仅仅是更新其值/属性