首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >轻敲时的UITableViewCell高度问题

轻敲时的UITableViewCell高度问题
EN

Stack Overflow用户
提问于 2018-06-12 19:27:36
回答 1查看 29关注 0票数 0

我在xib中使用了自定义的UITableViewCell。在UITableViewCell中,我使用了集合视图。所有的工作都很好,但是当我展开和折叠单元格时出现了一个问题。我想要做的是,当我点击单元格时,如果它不是折叠的,它就会变成展开。我改变了它的限制,但它不工作,它工作时,我点击按钮两次。我的代码是:

代码语言:javascript
复制
#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;
}

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

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

代码语言:javascript
复制
#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];

 }
EN

回答 1

Stack Overflow用户

发布于 2018-06-13 14:44:54

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

代码语言:javascript
复制
-(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()
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50816030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档