如何在按钮单击同一单元格内时隐藏/显示自定义tableview单元格内的特定视图

  • 我有一个table view (例如:20行)。 我使用了这个table viewcustom table view cell

  • 在这个table view cell ,有几个labels ,一个button和一个hidden view (UIView)

  • 我已经编写了用于hide/show custom table view cell class hidden view button action 。它工作正常。但它会影响到表视图中的其他rows 。 这意味着,当我点击第一行中的按钮,然后显示隐藏的视图时,它可以在scroll down时在表视图中的其他一些行中看到。

  • 同时(当hide/show ),我想increasedecrease行高(仅点击行/单元格)。 出了什么问题。 下面是我的代码和一些屏幕截图,以获得一个想法。

这是单击展开按钮时的第一行

在我滚动的同时,这是其他行/单元格的外观,显示隐藏视图而不单击展开按钮

注意:当单击每个单元格中的展开按钮时,单元格会自动展开/增加。

这就是我在custom table view cell类中hideshow hidden view

 - (IBAction)hideshow:(id)sender { BOOL ishidden = self.insideCollectionView.hidden; if(ishidden == true) { self.insideCollectionView.hidden = false; } else { self.insideCollectionView.hidden = true; } } 

出了什么问题,希望你对此有所帮助。

高级:如果有一种方法可以在单击每个单元格的展开按钮时隐藏/显示和扩展(增加行高度)。

我自己为此找到了合适的解决方案。 支持我的每个人。

做以下事情。

  1. 创建一个NSMutableArray以保存clicked Which row中的Which clicked
  2. 然后,当用户单击Custom table view cellButton时,检查index path是否alreadymutable array ,如果它already在其中,则将其romove ,否则添加它。
  3. 然后在cellforrowatindexpath方法中,检查nsmutable array并检查indexpath.row是否exist
  4. 最后,如果它exists ,不要隐藏,否则hide它,这是完美的。

这是表视图的实现。 .m文件

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 25; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { FirstTableViewCell *cells = [tableView dequeueReusableCellWithIdentifier:@"tvcell" forIndexPath:indexPath]; NSString *theIndexpath = [NSString stringWithFormat:@"%ld", (long)indexPath.row]; //here check whether it is exists or not. if ([chekcme containsObject:theIndexpath]) { cells.insideCollectionView.hidden = false; } else { cells.insideCollectionView.hidden = true; } [cells setColletionData:bbarray]; [cells.expandMe addTarget:self action:@selector(runs:) forControlEvents:UIControlEventTouchUpInside]; //in here set the tag of the button to indexpath.row cells.expandMe.tag = indexPath.row; return cells; } //this is the action for the button inside the custom tableview cell. - (IBAction)runs:(UIButton *)sender{ NSString *myob = [NSString stringWithFormat:@"%li", (long)sender.tag]; NSLog(@"%@",myob); if ([chekcme containsObject:myob]) { [chekcme removeObject:myob]; } else { [chekcme addObject:myob]; } NSLog(@"%@", chekcme); //keep in mind to reload the table view here. [self.maintableView reloadData]; } 

注意:checkme是NSMutableArray,用于保存用户单击的对象。

我需要在tableview数据源方法中查看代码,但我认为以下内容可以解决您的问题:

问题#1:我假设您正在为您的单元格使用deque,这会导致在滚动时重复使用单元格。 我建议你保持每个单元格的状态(例如:isExpanded)并在cellForRowAtIndex中相应地配置单元格:

问题#2:在heightForRowAtIndex:中使用相同的’IsExpanded’,并在为’IsExpanded’更改状态时为您的单元格调用reloadRowsAtIndexPaths:

该按钮影响多rows是因为您将BOOL存储在cellcollectionView中。

cell重新用于另一row ,将根据先前用于的row的状态hidden/showncell 。 正如已经建议的那样,您需要为每个rows存储此state ,并在dataSource准备cell时相应地设置它。

您应该首先在cellForRowAtIndexPath中设置有关视图的状态。 希望它会对你有所帮助。

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // here you have to maintain the status about the current cell. whether it is hidden or not. cell.insideCollectionView.hidden = status; } 

我,我正在做同样的事情:在didSelect

  switch selectedIndexPath { case nil: selectedIndexPath = indexPath default: if selectedIndexPath! == indexPath { selectedIndexPath = nil } else { selectedIndexPath = indexPath } } tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 

在行高

 if selectedIndexPath != nil { if selectedIndexPath == indexPath { return estimatedHeight } } return 44 

selectedIndexPath是NSIndexPath的属性。 希望这可以帮到你。

您需要重新加载所有表而不是单个单元格,您可以在代码下面使用

  tbl_name .beginUpdates( tbl_name .endUpdates() 

更新

 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { if indexPath.row == userselectedrow // save selectecd cell index if you want to only one cell expand otherwise save selected row in array and compare to here { return incrementheight // increment row height here } else { return decrementheight // decrement row height } } - (IBAction)hideshow:(UIButton*)sender { CGPoint point11=[sender convertPoint:CGPointZero toView:tbl_calender]; NSIndexPath index_pathGlobal =[tbl_calender indexPathForRowAtPoint:point11]; // save index_pathGlobal } 

希望这会对你有所帮助

步骤1 : – 假设您正在显示标签值,按钮和隐藏视图等行中的数据。在数组ex中添加一个参数。 isViewNeedToShow ,默认情况下填充该值为FALSE

步骤2 : – 在您的按钮操作之后,您将indexPath.row作为索引路径中行的单元格中的标记值传递,因此在按钮操作上更改数组值参数isViewNeedToShow == TRUE ,并重新加载表视图的部分。 例如: –

 Item *tempItem = yourArray[sender.tag]; tempItem. isViewNeedToShow = tempItem. isViewNeedToShow ? FALSE : TRUE; **For particular row update** :- [yourTableView beginUpdates]; [yourTableView reloadRowsAtIndexPaths:@[sender.tag, 0] withRowAnimation:UITableViewRowAnimationNone]; [yourTableView endUpdates]; 

第3步 : – 如果要扩展表格单元格,则必须计算包含视图,标签等项目的行高。

 Or if you are using auto layouts in your project use UI Table View Delegate Methods -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewAutomaticDimension; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewAutomaticDimension; } 

我希望你的问题能得到解决。