问题
I have a
table view
with (ex : 20 rows). and I have used acustom table view cell
with thistable view
.inside this
table view cell
, there are fewlabels
, onebutton
and ahidden view (UIView)
.I have written
button
action
forhide/show
thehidden view
inside thecustom table view cell class
.it works fine.but it affect to otherrows
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 whenscroll down
.At the same time (when
hide/show
), I want toincrease
anddecrease
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.
- Create a
NSMutableArray
to holdWhich
button
inWhich
row
clicked
. - Then when user click on the
Button
inCustom table view cell
, check it thatindex path
isalready
in themutable array
, if it isalready
inside it, thenromove
it ,otherwise add it. - Then in the
cellforrowatindexpath
method, check thatnsmutable array
and , check whether theindexpath.row
isexist
ornot
. - Finally, if it is
exists
, do not hide, elsehide
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