[英]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 view
的custom 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
),我想increase
和decrease
行高(仅点击行/单元格)。 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
类中hide
和show
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. 做以下事情。
NSMutableArray
to hold Which
button
in Which
row
clicked
. NSMutableArray
持有Which
button
在Which
row
clicked
。 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 cell
的Button
时,检查它是否already
在mutable array
中的index path
,如果它already
在其中,则将其romove
,否则添加它。 cellforrowatindexpath
method, check that nsmutable array
and , check whether the indexpath.row
is exist
or not
. cellforrowatindexpath
方法,检查nsmutable array
和,检查是否indexpath.row
是exist
或not
。 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
存储在cell
的collectionView
中。
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/shown
该cell
。 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.