如何在按钮单击同一单元格内时隐藏/显示自定义tableview单元格内的特定视图 [英] how to hide/show a specific view inside the custom tableview cell when button click inside the same cell
问题描述
-
我有一个
表格视图
with(例如:20行)。并且我使用自定义表格视图单元格
使用此表格视图
。
I have a
table view
with (ex : 20 rows). and I have used acustom table view cell
with thistable view
.
在此表格视图单元格
中,有少量标签
,一个按钮
和隐藏视图(UIView)
。
inside this table view cell
, there are few labels
, one button
and a hidden view (UIView)
.
I写了按钮
行动
为隐藏/显示
隐藏视图
在自定义表格视图单元格类
.it工作正常。但它会影响到其他行
在表格视图中。这意味着,当我点击第一行中的按钮,然后隐藏的视图显示,当向下滚动
时,它可以在表视图的其他一些行中看到。
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.
注意:当单击每个单元格中的展开按钮时,单元格会自动展开/增加。
这就是我隐藏
和 show
隐藏视图
,位于自定义表格视图单元格
类中。
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.
高级:如果有一种方法可以在单击展开按钮时同时隐藏/显示和扩展(增加行高)对于每个单元格。
推荐答案
为我自己找到合适的解决方案。支持我的每个人。
Found the suited solution for this by my self. thanx everyone who supported me.
执行以下操作。
- 创建
NSMutableArray
在中持有
哪个
按钮
哪个行
点击
。 - 然后当用户点击
自定义表格视图单元格
中的按钮
时,请检查索引路径
已经在
可变数组
中,如果它是已经
里面,然后romove
它,否则加上它。 - 然后在
cellforrowatindexpath
方法,检查nsmutable array
并检查indexpath.row
存在
或不
。 - 最后,如果它是
存在
,不要隐藏,否则隐藏
它,这很有效。
- 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.
这里是表视图的实现。 .m
file
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];
}
注意:checkme是NSMutableArray到保存用户点击的对象。
这篇关于如何在按钮单击同一单元格内时隐藏/显示自定义tableview单元格内的特定视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!