Ios 动态可扩展表视图

Ios 动态可扩展表视图,ios,objective-c,uitableview,customization,Ios,Objective C,Uitableview,Customization,我正在尝试实现一个可扩展的表格视图,比如标题和子标题视图。 在选择单元格时,它会扩展为几个包含正在解析的json数据的单元格。 要填充的数据是从JSON对象接收的。 有人能帮我吗?你可以使用uitableview方法 [UITableview beginUpdates]; // delete the row in table view [UITableview deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSectio

我正在尝试实现一个可扩展的表格视图,比如标题和子标题视图。 在选择单元格时,它会扩展为几个包含正在解析的json数据的单元格。 要填充的数据是从JSON对象接收的。
有人能帮我吗?

你可以使用
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];

希望它对您有用。

将您的类别创建为节标题,将子类别创建为特定节的行。使用此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];
}
-(NSInteger)节数表视图:(UITableView*)表视图
{
//计算节数==类别数
返回段计数;
}
-(UIView*)表格视图:(UITableView*)表格视图用于标题部分:(NSInteger)部分
{
//为节创建节标题。。。
//添加一个按钮,以便您可以监视用户是否点击了部分和
//设置按钮的标签=节
//配置剖面视图
返回页眉;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
//如果未点击节,则NumberOfSubcategories节=0
//否则
//子类别集合的计算数
返回NumberOfSubcategoriesForSection;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
//将与子类别共同响应的单元格出列
//配置单元
返回单元;
}
-(无效)BtnSectionPressed:(id)发送方{
//更新选定节的数据源子类别
//节==[发送者标签]
//[重新加载表格]
//还是更好的解决方案
[UITableview开始更新];
//上一节
//使用上次展开节的子类别索引路径数组调用
-(void)deleteRowsAtIndexPaths:(NSArray*)带有RowAnimation:(UITableViewRowAnimation)动画的InExpaths;
//扩展新部分
//使用要展开的节的子类别索引路径数组调用
-(void)InsertRowsAndExpaths:(NSArray*)InExpaths with RowAnimation:(UITableViewRowAnimation)动画
[UITableview EndUpdate];
}

在这里,您可以用swift轻松完成