如何在 UITableView 中实现可扩展 - 可折叠 UITableViewCell - IOS [英] How to implement Expandable - Collapsable UITableViewCell in UITableView - IOS
问题描述
我采用了 UITableview
的方法来获取单元格点击 ExpandView
.我想要实现这样的东西.
I took approach of the UITableview
to get cell click ExpandView
. I want something like this to be implement.
所以,UITableView
将是最好的方法或建议我任何好的实现方式,我也无法让 subview
根据 进行调整屏幕尺寸
.
So, UITableView
would be best approach for this or suggest me any good way of implementing, also I am not able to get subview
to adjust according to screenSize
.
推荐答案
可能还有其他方法可以实现这一点,但这就是我动态扩展 UITableViewCell
的方式.这可以提供想法,您可以实施您的解决方案.
Could be there are another ways to accomplish this but this is how I am expand UITableViewCell
on the fly. This could give the idea and you can implement your solution.
我在数据模型中保留行高:
I keep row heights in my data model:
- (void)viewDidLoad {
[super viewDidLoad];
//Data source
datasource = [[NSMutableArray alloc] init];
NSMutableDictionary *aDicti = [[NSMutableDictionary alloc] init];
[aDicti setValue:@"a TEXT" forKey:@"text"];
[aDicti setValue:@(50) forKey:@"cellheight"]; // here
}
选择更改时,仅在数据源中更新相关密钥.
When selection changed, just updating related key in data source.
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView beginUpdates];
[[datasource objectAtIndex:indexPath.row] setObject:@(50) forKey:@"cellheight"];
[tableView endUpdates];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView beginUpdates];
[[datasource objectAtIndex:indexPath.row] setObject:@(200) forKey:@"cellheight"];
[tableView endUpdates];
}
一次 [tableView endUpdates];执行 heightForRowAtIndexPath
和 numberOfRowsInSection
委托方法触发并使用数据源中的值自动调整单元格高度.
Once [tableView endUpdates]; executed heightForRowAtIndexPath
and numberOfRowsInSection
delegate methods fired and automatically adjust cell height with the value from data source.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [[[datasource objectAtIndex:indexPath.row] objectForKey:@"cellheight"] intValue];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return datasource.count;
}
如果您不想在数据源中保留行高,您基本上可以应用此方法.
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView beginUpdates];
[tableView endUpdates];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView beginUpdates];
[tableView endUpdates];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView.indexPathForSelectedRow.row == indexPath.row) {
return 100;
}
return 50;
}
这篇关于如何在 UITableView 中实现可扩展 - 可折叠 UITableViewCell - IOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!