0

在此处输入图像描述

如何像这样自定义 UITable:UITableView 包含很多行,每行包含很多子行。使用添加按钮添加子行?

4

4 回答 4

1

您可以创建这样的结构

[
    {
        "Title": "Section 1",
        "Rows": [
            {
                "Title": "Row 1",
                "Rows": [
                    {
                        "Title": "Sub Row 1", 
                        "Rows":[]
                    }
                ]
            }
        ]
    }
]

这是一个字典数组。每个部分将有一个行数组,每行将有一个子行数组。

源代码

于 2013-05-03T13:04:18.137 回答
0

您可以为此使用分组tableview,但它不会像图片中那样为您提供 iphone 的完整行宽。

如果您使用普通的表格视图,它应该会给您完整的行外观,但添加加号按钮可能会变得困难。

您需要有一个来源,可以说每个部分行的数组或字典

对于图像使用cell.imageView.image= yourimage;

对于箭头使用cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

为此(10),您需要使用 cell.accessoryView= yourcustomview

//create multiple section according to your picture it is 2 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //return an integer here from a source,variable or just return static int 
    return 2; 
}
//add plus button for each section
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
// Create header view and add label as a subview
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 400, 320, 400)];

    //add a button to open edit
    UIButton *declineButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [declineButton setTag:section];
    declineButton.titleLabel.tag=1;
    [declineButton setFrame:CGRectMake(450,50,70,40)];
    [declineButton setTitle:@"Edit" forState:UIControlStateNormal];
    UIImage *declinebuttonImage = [[UIImage imageNamed:@"orangeButton@2x.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 20, 20, 20)];
    [declineButton setBackgroundImage:declinebuttonImage forState:UIControlStateNormal];
    [declineButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [declineButton addTarget:self action:@selector(declineEvent:) forControlEvents:UIControlEventTouchUpInside];

    [view addSubview:declineButton];
    return view;
}

- (IBAction)declineEvent:(id)sender {
    UIButton *button = (UIButton *)sender;
    NSInteger row = button.tag;
   NSInteger column = button.titleLabel.tag;
    //NSIndexPath *selectedSection= [NSIndexPath indexPathForRow:column inSection:row];
    NSLog(@"Row is %i column is %i",row,column);
    //populate your source array for the section according to your needs here 

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return yoursourceforthissection;

}
于 2013-05-03T13:14:19.677 回答
0

我认为您应该使用具有表格视图的自定义表格视图单元格。

http://www.appcoda.com/customize-table-view-cells-for-uitableview/

检查这个希望这会对你有所帮助。

于 2013-05-03T13:26:41.923 回答
0

我在 Github 上有一个与此类似的项目。基本上它是一个带有可扩展部分的 UITableView。以下步骤使它对我有用:

  1. NSMutableDictionary对于dataSource, dict 包含NSMutableArrays作为键的每一行的值。
  2. 您将拥有另一个NSMutableArray与您的NSMutableDictionary. 这NSMutableArray包含BOOL值,YES代表展开的部分和NO折叠的部分。在我的情况下,默认情况下所有部分都是折叠的。3.tableView 中的section 数量就是你的NSMutableDictionary.
  3. 如果您NSMutableArrayBoolYES在某个 indexpath 处包含 a,则此部分的行数是您从NSMutableDictionary. 否则,此部分将只有 1 行。

例子:

if ([[boolArray objectAtIndex:section] boolValue]) {
    return [[yourDict valueForKey:yourKeyForIndexPAth] count];
}else{
    return 1;
}

只需检查一下并随意使用代码:

GitHub 链接

于 2013-05-03T14:08:25.283 回答