2013-05-03 62 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