-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 50;
}
In this place, i would like to add a button dynamically to increase the height of the cell, so when user clicks upon that button it should be increase the height of cell and then click again to resize the height.
在这里,我想动态添加一个按钮来增加单元格的高度,因此当用户单击该按钮时,它应该增加单元格的高度,然后再次单击以调整高度。
I want to something like :
我想说的是:
-(void)IncreaseCell
{
UIButton *DownArrow = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[DownArrow addTarget:self
action:@selector(IncreaseCell:)
forControlEvents:UIControlEventTouchDown];
//[DownArrow setTitle:@"Arrow" forState:UIControlStateNormal];
DownArrow.frame = CGRectMake(121.0, 112.0, 72.0, 37.0);
[cell.contentView addSubview:DownArrow];
UIImage *buttonDown = [UIImage imageNamed:@"friendsDownArrowButton.png"];
[DownArrow setBackgroundImage:buttonDown forState:UIControlStateNormal];
[cell.contentView addSubview:DownArrow];
}
4 个解决方案
#1
1
-
You will need to create a
NSMutableArray
as an instance variable, in which you keep track of all the cells, that you want to have "increased".您将需要创建一个作为实例变量的NSMutableArray,其中跟踪您想要“增加”的所有单元格。
@interface YourTableViewController : UITableViewController { NSMutableDictionary *increasedRows; }
Remember to alloc/init that variable.
记住要分配/初始化那个变量。
-
To get your cell increased:
增加你的细胞数量:
-(void)increseCell: (BOOL)status forIndexPath: (NSIndexPath*)indexPath { [increasedRows setObject:[NSNumber numberWithBool:status] forKey: indexPath]; [self.tableView beginUpdates]; //Delete if you don't want it animated [self.tableView endUpdates]; //Delete if you don't want it animated // [self.tableView reloadData]; //Uncomment if you don't want it animated }
-
You change your
tableView: heightForRowAtIndexPath:
declaration to check this dictionary for your indexPath.您可以更改tableView: heightForRowAtIndexPath:声明以检查字典中的indexPath。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { // Check if cell is increased, return custom height if([[increasedRows objectForKey:indexPath] boolValue]) { return 150; } // Cell isn't increased so return regular height return 50; }
This method will allow you to do it for every row individually and allows you to animate it.
这个方法将允许您对每一行单独执行它,并允许您对它进行动画处理。
#2
0
You need to change your implementation of heightForRowAtIndexPath
.
您需要更改heightForRowAtIndexPath的实现。
Add some storage, like an array, which holds flags indicating if a row is expanded or not. When a button is tapped, get the index path of the cell that it is on and edit the array contents to set / unset the flag. Reload the table view (or the row for the changed index path).
添加一些存储,如数组,它包含指示是否展开行的标志。单击按钮时,获取它所在单元格的索引路径,并编辑数组内容以设置/取消标记。重新加载表视图(或已更改索引路径的行)。
#3
0
All you have to do is declare a variable:
你所要做的就是声明一个变量:
@interface yourViewController ()
{
CGFloat cellSize;
}
And in your heightForRowAtIndexPath:
:
和你heightForRowAtIndexPath::
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return cellSize;
}
in your viewDidLoad
在你viewDidLoad
-(void)viewDidLoad
{
[super viewDidLoad];
cellSize = 50;
}
and last in your increaseCell:
最后在你的增长:
-(void)IncreseCell {
cellSize += 10;
[self reloadData];
}
#4
0
what i did in one of my project is i created a custom cell and add subview to it statically
在我的一个项目中,我创建了一个自定义单元格并静态地向它添加子视图
1 st scene non collapsed view custom cell shows only button on click of which it will show extended view by increasing size of cell..
1 st scene non - view custom cell show only button on click of which it will show extended view by size of cell..
i.e on click of button you should increse size of cell using function..
我。点击按钮时,你应该使用函数增加单元格的大小。
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
BOOL check=[[booleanArrayofrows objectAtIndex:indexPath.row] boolValue];
if (check)
{
return 180; ..expanded view height
}
return 40; collapse view height
}
}
#1
1
-
You will need to create a
NSMutableArray
as an instance variable, in which you keep track of all the cells, that you want to have "increased".您将需要创建一个作为实例变量的NSMutableArray,其中跟踪您想要“增加”的所有单元格。
@interface YourTableViewController : UITableViewController { NSMutableDictionary *increasedRows; }
Remember to alloc/init that variable.
记住要分配/初始化那个变量。
-
To get your cell increased:
增加你的细胞数量:
-(void)increseCell: (BOOL)status forIndexPath: (NSIndexPath*)indexPath { [increasedRows setObject:[NSNumber numberWithBool:status] forKey: indexPath]; [self.tableView beginUpdates]; //Delete if you don't want it animated [self.tableView endUpdates]; //Delete if you don't want it animated // [self.tableView reloadData]; //Uncomment if you don't want it animated }
-
You change your
tableView: heightForRowAtIndexPath:
declaration to check this dictionary for your indexPath.您可以更改tableView: heightForRowAtIndexPath:声明以检查字典中的indexPath。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { // Check if cell is increased, return custom height if([[increasedRows objectForKey:indexPath] boolValue]) { return 150; } // Cell isn't increased so return regular height return 50; }
This method will allow you to do it for every row individually and allows you to animate it.
这个方法将允许您对每一行单独执行它,并允许您对它进行动画处理。
#2
0
You need to change your implementation of heightForRowAtIndexPath
.
您需要更改heightForRowAtIndexPath的实现。
Add some storage, like an array, which holds flags indicating if a row is expanded or not. When a button is tapped, get the index path of the cell that it is on and edit the array contents to set / unset the flag. Reload the table view (or the row for the changed index path).
添加一些存储,如数组,它包含指示是否展开行的标志。单击按钮时,获取它所在单元格的索引路径,并编辑数组内容以设置/取消标记。重新加载表视图(或已更改索引路径的行)。
#3
0
All you have to do is declare a variable:
你所要做的就是声明一个变量:
@interface yourViewController ()
{
CGFloat cellSize;
}
And in your heightForRowAtIndexPath:
:
和你heightForRowAtIndexPath::
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return cellSize;
}
in your viewDidLoad
在你viewDidLoad
-(void)viewDidLoad
{
[super viewDidLoad];
cellSize = 50;
}
and last in your increaseCell:
最后在你的增长:
-(void)IncreseCell {
cellSize += 10;
[self reloadData];
}
#4
0
what i did in one of my project is i created a custom cell and add subview to it statically
在我的一个项目中,我创建了一个自定义单元格并静态地向它添加子视图
1 st scene non collapsed view custom cell shows only button on click of which it will show extended view by increasing size of cell..
1 st scene non - view custom cell show only button on click of which it will show extended view by size of cell..
i.e on click of button you should increse size of cell using function..
我。点击按钮时,你应该使用函数增加单元格的大小。
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
BOOL check=[[booleanArrayofrows objectAtIndex:indexPath.row] boolValue];
if (check)
{
return 180; ..expanded view height
}
return 40; collapse view height
}
}