简体   繁体   English

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

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

  • I have a table view with (ex : 20 rows). 我有一个table view (例如:20行)。 and I have used a custom table view cell with this table view . 我使用了这个table viewcustom table view cell

  • inside this table view cell , there are few labels , one button and a hidden view (UIView) . 在这个table view cell ,有几个labels ,一个button和一个hidden view (UIView)

  • I have written button action for hide/show the hidden view inside the custom table view cell class .it works fine.but it affect to other rows in the table view. 我已经编写了用于hide/show custom table view cell class hidden view button action 。它工作正常。但它会影响到表视图中的其他rows that means, when I tap the button in first row, then the hidden view show, and it can see in some other rows in the table view when scroll down . 这意味着,当我点击第一行中的按钮,然后显示隐藏的视图时,它可以在scroll down时在表视图中的其他一些行中看到。

  • At the same time (when hide/show ), I want to increase and decrease the row height (only clicked row/cell) . 同时(当hide/show ),我想increasedecrease行高(仅点击行/单元格)。 what is going wrong. 出了什么问题。 below is my codes and some screen shots to get an idea. 下面是我的代码和一些屏幕截图,以获得一个想法。

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

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

note : cell expand/increase it self when click on expand button in each cell. 注意:当单击每个单元格中的展开按钮时,单元格会自动展开/增加。

this is how I hide and show the hidden view , inside the custom table view cell class. 这就是我在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;
    }
}

what is going wrong, hope your help with this. 出了什么问题,希望你对此有所帮助。

Advance : it is great if there is a way to do both hide/show and expand(increase the row height) of the cell when click on expand button for each cell. 高级:当单击每个单元格的展开按钮时,有一种方法可以同时隐藏/显示和扩展(增加行高)。

Found the suited solution for this by my self. 我自己为此找到了合适的解决方案。 thanx everyone who supported me. 感谢所有支持我的人。

Do the followings. 做以下事情。

  1. Create a NSMutableArray to hold Which button in Which row clicked . 创建一个NSMutableArray持有Which buttonWhich row clicked
  2. Then when user click on the Button in Custom table view cell , check it that index path is already in the mutable array , if it is already inside it, then romove it ,otherwise add it. 然后,当用户单击Custom table view cellButton时,检查它是否alreadymutable array中的index path ,如果它already在其中,则将其romove ,否则添加它。
  3. Then in the cellforrowatindexpath method, check that nsmutable array and , check whether the indexpath.row is exist or not . 然后在cellforrowatindexpath方法,检查nsmutable array和,检查是否indexpath.rowexistnot
  4. Finally, if it is exists , do not hide, else hide it, this works perfectly. 最后,如果它exists ,不要隐藏,否则hide它,这是完美的。

here is the implementation for the table view. 这是表视图的实现。 .m file .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. 注意:checkme是NSMutableArray,用于保存用户单击的对象。

I will need to see the code in your tableview data source methods, but i think following can solve your issues: 我需要在tableview数据源方法中查看代码,但我认为以下内容可以解决您的问题:

Issue #1: I am assuming your are using deque for your cell, this is causing the cell to be reused when you scroll. 问题#1:我假设您正在为您的单元格使用deque,这会导致在滚动时重复使用单元格。 I will suggest you to maintain the state of your each cell (eg: isExpanded) and configure cell accordingly in cellForRowAtIndex: 我建议你保持每个单元格的状态(例如:isExpanded)并在cellForRowAtIndex中相应地配置单元格:

Issue #2: Use the same 'IsExpanded' in heightForRowAtIndex: and call reloadRowsAtIndexPaths: on the table for your cell when state is changed for 'IsExpanded' 问题#2:在heightForRowAtIndex:中使用相同的'IsExpanded'并在为'IsExpanded'更改状态时为您的单元格调用reloadRowsAtIndexPaths:

you have need to reload all table instead of single cell you can use below code 您需要重新加载所有表而不是单个单元格,您可以在代码下面使用

 tbl_name .beginUpdates(
 tbl_name .endUpdates()

Update 更新

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
}

hope this will be help you 希望这会对你有所帮助

You should set the status about the view initially in cellForRowAtIndexPath. 您应该最初在cellForRowAtIndexPath中设置有关视图的状态。 Hope it will help you. 希望它会对你有所帮助。

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

That the button affects several rows is because you store the BOOL in the collectionView in the cell . 该按钮影响多rows是因为您将BOOL存储在cellcollectionView中。

When the cell is reused for another row , that cell will be hidden/shown based on the status of the row it was previously used for. cell被重用于另一row ,将根据先前用于的row的状态hidden/showncell As has already been suggested you need to store this state for each and every one of your rows and set it accordingly when the cell is prepared by the dataSource . 正如已经建议的那样,您需要为每个rows存储此state ,并在dataSource准备cell时相应地设置它。

I,am doing the same thing like: In didSelect 我正在做同样的事情:在didSelect中

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

And at row height 在行高

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

The selectedIndexPath is a property of NSIndexPath. selectedIndexPath是NSIndexPath的属性。 Hope this may help you. 希望这可以帮到你。

Step 1 :- Assume that you are showing data in rows like label value, button and hidden view.Add one parameter in your array ex. 步骤1 : - 假设您在标签值,按钮和隐藏视图等行中显示数据。在数组ex中添加一个参数。 isViewNeedToShow , By default fill that value FALSE . isViewNeedToShow ,默认情况下填充该值为FALSE

Step 2 :- After that in your button action your are passing indexPath.row as tag value in cell for row at index path, So on button action change the array vale parameter isViewNeedToShow == TRUE , and reload the section of table view. 步骤2 : - 在您的按钮操作之后,您将indexPath.row作为索引路径中行的单元格中的标记值传递,因此在按钮操作上更改数组值参数isViewNeedToShow == TRUE ,并重新加载表视图的部分。 For ex:- 例如: -

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

Step 3 :- If you want to expand table cell you have to calculate height of row that contains the items like view, label etc. 第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;
    }

I hopes your problem would be resolved. 我希望你的问题能得到解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何显示通过滑动行在自定义tableview单元内创建的视图? - How to show a view created inside the custom tableview cell by swiping the row? Ios 在 tableview 单元格内显示/隐藏视图 - Ios show/hide views inside a tableview cell 在tableview单元格内单击时更改按钮 - change button on click inside tableview cell 如何在swift 3中的tableview单元格内单击更改按钮标题 - how to change the button title on click inside a tableview cell in swift 3 我如何单击UI表格视图单元格(自定义单元格)内的按钮,然后转到其他ViewController - how can I click button inside UI table view cell (custom cell ) and then goto other viewcontroller 单击同一单元格内的按钮时,如何折叠和展开表格视图单元格的某些部分? - How to Collpase and Expand some part of a table view cell when a button inside the same cell is clicked? 如何在tableview单元格内加载内容视图? - How to load content view inside a tableview cell? 如何在tableview单元格内的collectionview单元格的按钮单击时获取索引 - How to get index on button click of collectionview cell which is inside tableview cell 在自定义tableview单元格中弹出 - Popover inside a custom tableview cell 在 UICollectionView 单元格内的录音按钮处显示视图 - Show view at taped button inside UICollectionView Cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM