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

旧巷老猫 提交于 2019-12-21 06:32:10

问题


  • I have a table view with (ex : 20 rows). and I have used a custom table view cell with this table view.

  • inside this table view cell, there are few labels, one button and a 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. 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.

  • At the same time (when hide/show), I want to increase and decrease the row height (only clicked row/cell) . 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.

- (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.


回答1:


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.
  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.
  3. Then in the cellforrowatindexpath method, check that nsmutable array and , check whether the indexpath.row is exist or not.
  4. Finally, if it is exists, do not hide, else hide it, this works perfectly.

here is the implementation for the table view. .m file

- (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.




回答2:


I will need to see the code in your tableview data source methods, but i think following can solve your issues:

Issue #1: I am assuming your are using deque for your cell, this is causing the cell to be reused when you scroll. I will suggest you to maintain the state of your each cell (eg: isExpanded) and configure cell accordingly in cellForRowAtIndex:

Issue #2: Use the same 'IsExpanded' in heightForRowAtIndex: and call reloadRowsAtIndexPaths: on the table for your cell when state is changed for 'IsExpanded'




回答3:


That the button affects several rows is because you store the BOOL in the collectionView in the cell.

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. 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.




回答4:


You should set the status about the view initially in 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;
    }



回答5:


I,am doing the same thing like: In 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. Hope this may help you.




回答6:


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




回答7:


Step 1:- Assume that you are showing data in rows like label value, button and hidden view.Add one parameter in your array ex. isViewNeedToShow, By default fill that value 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. 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.

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.



来源:https://stackoverflow.com/questions/39484152/how-to-hide-show-a-specific-view-inside-the-custom-tableview-cell-when-button-cl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!