【问题标题】:Dynamically expandable table view动态可扩展的表格视图
【发布时间】:2015-09-30 10:47:02
【问题描述】:
我正在尝试实现一个可扩展的表格视图单元格。例如标题和子标题视图。
在选择单元格时,它会扩展为更多带有正在解析的 json 数据的单元格。
要填充的数据是从 JSON 对象接收的。
谁能帮我解决这个问题?
【问题讨论】:
标签:
ios
objective-c
uitableview
customization
【解决方案1】:
你可以使用uitableview方法
[UITableview beginUpdates];
// delete the row in table view
[UITableview deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:lastcount - 1]] withRowAnimation:UITableViewRowAnimationTop];
// insert section in table Or Set as per your need
[UITableview insertSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(Array.count - [[results objectForKey:@"data"] count], [[results objectForKey:@"data"] count])] withRowAnimation:UITableViewRowAnimationFade];
[UITableview endUpdates];
希望它对你有用。
【解决方案2】:
将您的类别创建为部分标题,并将您的子类别创建为特定部分的行。使用这个 UITableView 委托方法:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// calculate section count == number of categories
return sectionCount;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// create section header for section...
// add a button so you can monitor if user tapped on section and
// set button's tag = section
// configure section view
return vwSectionHeader;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// if section not tapped than numberOfSubcategoriesForASection = 0
// else
// calcualate numberOfSubcategoriesForASection
return numberOfSubcategoriesForASection;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// dequeue your cell that corespondes to the subcategory
// configure cell
return cell;
}
-(void)BtnSectionPressed:(id)sender{
// update data source subcategories for selected section
// section == [sender tag]
// [reload table]
// or nicer solution
[UITableview beginUpdates];
// colapse previous section
// call with array of subcategories index paths for section last expanded
- (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
// expand new section
// call with array of subcategories index paths for section you want to expand
- (void)insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
[UITableview endUpdates];
}