【问题标题】:how to create single tableview with two nested tableview cell xibs using iOS storyboard?如何使用 iOS 故事板创建具有两个嵌套 tableview 单元格 xib 的单个 tableview?
【发布时间】:2015-12-10 05:12:02
【问题描述】:

我正在尝试使用父子自定义表格视图单元格实现手风琴表格视图。我正在使用下面提到的开源代码。

源码:https://github.com/singhson/Expandable-Collapsable-TableView

在该代码中具有单个 tableview 和单个 tableview 单元格。它将显示父单元格和子单元格,但我想制作:

  1. 主故事板 Tableview
  2. Parenttableviewcell 分离类和xib
  3. Childtableviewcell 分离类和xib

它应该适用于带有手风琴的主控制器表视图。现在在此代码中没有单独的自定义单元格(父和子使用相同的 tableview 单元并仅更改数据)。

【问题讨论】:

  • 你能不能给一张图片或其他东西来解释你想要达到的目标?,没有嵌套的tableview单元格,也许你想要在每个单元格中添加另一个tableviewcontroller?
  • 我会补充的。给我两分钟!@Tj3n
  • 请看上面我想现在你可以理解@Tj3n
  • 试试这个SO Question,它有很多很好的答案,不过你应该遵循第二个答案,即使苹果公司也得到了this tutorial 来了解如何实现这一目标
  • 所有东西都不是 Xib 的。它对我来说很复杂。任何简单的方法来做到这一点!

标签: ios objective-c uitableview


【解决方案1】:

您可以通过尝试管理表格视图的部分和行来实现它,我认为这是在不使用任何第三方代码的情况下实现它的最简单方法。例如

  • 所有节标题都将包含自己的行数。
  • 获取一个数组或字典或数组,以存储分段中展开行的状态。 (说“1”表示展开状态,“0”表示折叠状态)
  • 如果在初始状态下,所有 UI 都展开,然后将数组或字典中的所有对象设置为“1”。
  • 点击单个标题(我假设单击部分的单个标题时部分将被折叠),您可以获得需要折叠的部分。对于数组或字典中的折叠部分行,将此状态保留为“0”。
  • 重新加载表并检查数组或字典中“0”实体的 heightForRow 委托方法。无论您发现它在哪里是 '0',都在委托方法中将高度返回为 0。
  • 对反向功能执行相反的操作。

代码更新

- (IBAction)btnMenuViewTypeTapped:(id)sender{
UIButton *btnSender = (UIButton *)sender;

if(menuViewType == MVTCollapse){
    [btnSender setImage:[UIImage imageNamed:@"MenuCollapse"] forState:UIControlStateNormal];
    menuViewType = MVTExpand;
    for(NSInteger intAtIndex=0; intAtIndex<[mutArrSearchMenuItems count]; intAtIndex++){
        [mutArrSectionOpened replaceObjectAtIndex:intAtIndex withObject:@"1"];
    }
} else{
    [btnSender setImage:[UIImage imageNamed:@"MenuExpand"] forState:UIControlStateNormal];
    menuViewType = MVTCollapse;
    for(NSInteger intAtIndex=0; intAtIndex<[mutArrSearchMenuItems count]; intAtIndex++){
        [mutArrSectionOpened replaceObjectAtIndex:intAtIndex withObject:@"0"];
    }
}
[tblViewRestaurantMenu reloadData];

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [mutArrSearchMenuItems count];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSInteger intNumOfRow;
    if([mutArrSectionOpened[section] isEqualToString:@"1"]){
        intNumOfRow = [mutArrSearchMenuItems[section][strMenuType] count];
    }
    else{
        intNumOfRow = 0;
    }
    return intNumOfRow;
}

【讨论】:

  • 好友非常感谢您的帮助顺便说一句我是新手。请提供一些代码。这对我很有帮助。我为这个问题挣扎了 3 天。@Rahul Mathur
  • 等待 10 分钟,让我用代码编辑答案。
  • @Apple_Ajay 抱歉耽搁了,请检查更新后的代码 :)
  • 兄弟它令人困惑。我需要合并两个单独的 tablview 单元格并扩展手风琴类型@Rahul
  • @Apple_Ajay 首先,您不需要两个单独的 XIB 来执行您的要求。与故事板一起去。不要担心单元格合并,因为您需要管理的是该特定部分的行数。你只需要玩那个方法,没有别的。
【解决方案2】:

您可以使用 SKSTableview 来实现它。

请参考以下链接:

  1. SKSTableView

【讨论】:

  • 您好,请问可以发到邮箱吗?
【解决方案3】:

让我们制作两种自定义单元格。

+父单元格为tableview sectionHeader

+子细胞是正常细胞

/*your datasource like this
 arrData = @[section, section, section ....]

section.name
section.image
section.arrayChild


 arrayChild = @[child, child, child....]
 child.name
 child.image
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return arrData.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    section = arrData[section];
    child = section.child;
    return child.count;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //Cell is Custom of UITableViewHeaderFooterViewCell
    //load your Parent Cell here

}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //load your custom chill cell here
}

【讨论】:

  • 请提供一些代码。我是这个@sticker 的新手
  • 非常感谢。顺便说一句,部分可以添加标签和按钮吗?
  • 您制作了一个自定义 UITableViewHeaderFooterView,您可以添加任何您想要的内容,例如另一个 UIView
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-23
  • 1970-01-01
相关资源
最近更新 更多