我在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;
}注意:有一个以上的自定义单元格,由条件管理,但我需要可扩展的单元格,特别是自定义单元格,这是写在上面的代码。
当我点击两次时,上面的代码就可以工作了。在单元格中有一个按钮,我在该按钮上设置目标,并使用目标I重新加载表。其代码为
#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];
}发布于 2018-06-13 14:44:54
更改showReward方法中的constraint值,并调用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()
}https://stackoverflow.com/questions/50816030
复制相似问题