点击

时间:2018-06-12 11:27:36

标签: objective-c uitableview uicollectionview

我使用了带有xib的自定义UITableViewCell。在UITableViewCell中,我使用了集合视图。所有事情都很好但是当我扩展和折叠单元格时会出现问题。我想要做的是当我点击单元格时,如果它没有被折叠,它就会变得膨胀。我更改了约束,但它没有工作,当我点按两次按钮时,它正在工作。我的代码是:

#pragma mark:tbl view delegate data source
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSLog(@"%lu",(unsigned long)postArr.count);
    return postArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    rewardCell*cell = [tableView dequeueReusableCellWithIdentifier:@"simpleCellReward"];

     if (cell == nil) {
         cell = [[rewardCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"simpleCellReward"];
     }

     [cell.showRewardBtn addTarget:self action:@selector(showRewards:) forControlEvents:UIControlEventTouchUpInside];

     cell.showRewardBtn.tag = indexPath.row+1;

     cell.collectionContainer.layer.cornerRadius = 3.0;
     cell.collectionContainer.clipsToBounds = true;

     cell.lblTitle.text = [NSString stringWithFormat:@"My Reward %ld",(long)indexPath.row];

     if (selectIndexForReward == indexPath.row) {
         if (cell.collectionHeightConstraint.constant == 0) {
             cell.collectionHeightConstraint.constant = 97;

         }else{
             cell.collectionHeightConstraint.constant = 0;
         }
     }

     [cell layoutIfNeeded];
     [cell.collectionView layoutIfNeeded];
     return cell;
}



-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ((int)indexPath.row == (slot-2)) {
        page = page+1;
        slot = slot+10;

       HUD = [MBProgressHUD showHUDAddedTo:self.view animated:true];

        [apiObj getPostData:@{@"page":[NSString stringWithFormat:@"%d",page],@"user_id":user.usrid}];
    }
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}

注意:有一个以条件管理的自定义单元格,但我需要可扩展单元格,特别是自定义单元格,在上面的代码中写入。

当我点击两次时,上面的代码工作正常。在单元格中有一个按钮,我在其上设置目标并使用该目标重新加载表。代码是

#pragma mark:ShowReward
-(void)showRewards:(UIButton*)sender{
    selectIndexForReward= (int)sender.tag-1;
    //[self.tblView reloadData];
    rewardCell *cell =(rewardCell *)sender.superview.superview.superview; //change with your class
    NSIndexPath *indexPath = [self.tblView indexPathForCell:cell];
    [self.tblView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];

 }

1 个答案:

答案 0 :(得分:0)

更改showReward方法中的约束值,并调用tableview的beginUpdate()endUpdate()方法。

-(void)showRewards:(UIButton*)sender{

rewardCell *cell =(rewardCell *)sender.superview.superview.superview; //change with your class

tableView.beginUpdates()
         if (cell.collectionHeightConstraint.constant == 0) {
             cell.collectionHeightConstraint.constant = 97;

         }else{
             cell.collectionHeightConstraint.constant = 0;
         }
     }
tableView.endUpdates()
}