如何在同一单元格内单击按钮时 hide/show 自定义 tableview 单元格内的特定视图

how to hide/show a specific view inside the custom tableview cell when button click inside the same cell

注意:单击每个单元格中的展开按钮时,单元格 expand/increase 会自动显示。

我是这样hideshowhidden view,里面的custom table view cellclass.

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

出了什么问题,希望得到您的帮助。

Advance :如果有一种方法可以在单击每个单元格的展开按钮时同时执行 hide/show 和展开(增加行高)单元格,那就太好了。

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

问题 #1:我假设您正在为您的单元格使用双端队列,这会导致在您滚动时重复使用该单元格。我建议您维护每个单元格的状态(例如:isExpanded)并在 cellForRowAtIndex:

中相应地配置单元格

问题 #2:在 heightForRowAtIndex: 中使用相同的 'IsExpanded',并在 table 上为您的单元格调用 reloadRowsAtIndexPaths:当 'IsExpanded'

的状态更改时

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

 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
}

希望对您有所帮助

您应该首先在 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;
    }

那个按钮影响了几个rows是因为你在cell中存储了BOOLcollectionView中。

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

我正在做同样的事情: 在 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

所选的 IndexPath 是 NSIndexPath 的 属性。 希望对您有所帮助。

第 1 步:- 假设您在行中显示数据,例如标签值、按钮和隐藏的 view.Add 数组 ex 中的一个参数。 isViewNeedToShow,默认填写那个值FALSE

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

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 步:- 如果你想展开 table 单元格,你必须计算包含视图、标签等项目的行的高度

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;
    }

希望您的问题得到解决

我自己找到了适合的解决方案。感谢所有支持我的人。

执行以下操作。

  1. 创建一个 NSMutableArray 以在 Which row 中容纳 Which button clicked.
  2. 然后当用户点击Custom table view cell中的Button时,检查index path是[=21=中的already,如果是already里面,然后romove,否则加上。
  3. 然后在cellforrowatindexpath方法中,检查nsmutable array和,检查indexpath.rowexist还是not
  4. 最后,如果是exists,不要隐藏,否则hide,这个完美。

这里是 table 视图的实现。 .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];
}

note : checkme is NSMutableArray to holds the objects clicked by the user.