2013-05-03 63 views
0

enter image description here如何自定义UITableView包含多行,每行包含多个subrow?

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

+0

为了使问题清晰。你也有章节礼节,一节有很多行,每行都有正确的子行。可以添加一行,同样可以添加一个子行,我是否正确? – Anupdas 2013-05-03 12:59:24

+0

是的,这是正确的 – DungProton 2013-05-03 13:00:50

回答

1

您可以创建一个结构类似这样的

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

这是字典的数组。每个部分都有一个行数组,每行都有一个数组。

Source Code

+0

你能解释更多吗? – DungProton 2013-05-03 13:06:35

+0

@DungProton我会为你制定一个演示代码。 – Anupdas 2013-05-03 13:08:03

+0

@DungProton我把我的答案放在演示源代码。它只是显示你如何显示这样的数据。您可以制定如何向每个部分添加新行的代码。 – Anupdas 2013-05-03 13:49:43

0

您可以使用分组tableview为,但它不会给你的iPhone全行的宽度就像在你的图片。

如果你使用普通的tableview,它应该给你全行的外观,但添加加号按钮可能会变得困难。

你需要有一个来源可以说每个部分行

对于图像使用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:@"[email protected]"] 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; 

} 
0

我有一个类似的项目,这在Github上。 基本上它是一个可扩展的部分UITableView。 以下步骤使其成为我工作:

  1. NSMutableDictionarydataSource,该字典包含NSMutableArrays作为每一行的键值。
  2. 您将有另一个NSMutableArray与您的NSMutableDictionary大小相同。此NSMutableArray包含BOOL值,YES代表展开的部分,NO代表折叠部分。在我的案例中,默认情况下所有部分都是折叠的。 3.您的tableView中的部分数量是您的NSMutableDictionary的大小。
  3. 如果您的NSMutableArrayBool值在某个索引路径中包含YES,则此部分的行数是您从NSMutableDictionary获取的索引路径值的数组大小。否则,你将只有1行此部分。

例子:

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

刚检查出来UND随意使用的代码:

GitHub Link