2012-07-27 56 views
1

我創建了一個完全由四個不同分組組成的UITableView。每個部分/組具有不同數量的行。我已經爲底部三組靜態添加了必要數量的行和文本框。但是,我不知道頂部部分/組需要多少行。我已經建立了頂級組,只是在.xib中有一個單元格,但每次加載視圖時,我都必須檢查需要多少個單元格,然後動態地將必要數量的行添加到頂部部分。如何實現具有靜態和動態單元的UITableView - IOS

我已經看了看周圍的StackOverflow,並看到人們討論insertRowsAtIndexPaths方法,但我一直無法讓它工作。

[settingsPageTableView beginUpdates]; 
[settingsPageTableView insertRowsAtIndexPaths:tempArray withRowAnimation:UITableViewRowAnimationAutomatic]; 
[settingsPageTableView endUpdates]; 

我是否需要在insertRowsAtIndexPaths方法中放置一些特定的代碼?我在一個NSMutableArray通過像這樣:

NSIndexPath *indexPath0 = [NSIndexPath indexPathForRow:1 inSection:0]; 
    NSMutableArray *tempArray=[NSMutableArray array]; 
    [tempArray addObject:indexPath0]; 

如果任何人有如何在一個UITableView的靜態和動態的單元格合併一無所知,這將是非常感謝!對於任何熟悉iPhone上常規設置的藍牙頁面的人來說,這正是我期待做的。它具有可變數量的設備以及頂部包含UISwitch的靜態單元。有誰知道如何做到這一點?

非常感謝!

回答

2

所以這裏是我剛纔想到的解決方法。我知道肯定永遠不會有超過7個單元,我將永遠需要UITable視圖的頂部。那麼,我可以在.xib文件中靜態設置這7個。從那裏,我可以只顯示我需要用這個具體數目:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 


    switch(section) { 
     case 0: 
      //Do a bunch of checking here on how many i actually need. Then, I will just return the required amount. As long as it is less than the number of cells I have statically placed in my .xib, everything works good. 
      return 4; 
     case 1: 
      return 3; 
     case 2: 
      return 1; 
     case 3: 
      return 2; 
     case 4: 
      return 3; 
     default: 
      return 0; 
     } 
//Every case but the first always needs the same amount of cells, so those are no problem. 
} 
3

我認爲你應該動態地做到這一點,只是因爲你在藍牙頁面上描述的單元總是存在並不意味着它是一個靜態單元,只是總是動態地創建。

+1

謝謝你,這是一個很好的觀點。我其實只是找到了一個解決方法。我現在就發佈它。 – 2012-07-27 00:23:05

+0

你值得投票最多分fosho哈哈 – 2012-07-27 00:23:35

+0

哈謝謝,但你是怎麼做到的?你會在這裏發佈嗎? – PhillM 2012-07-27 00:26:06

1

最佳和最簡單的方法:在一個動態的TableView 2的頭

1)將容器視圖)指定靜態TableView到此容器並取消「滾動已啓用」

+1

最好描述http://stackoverflow.com/a/22524085/1537178(同一作者) – 2017-03-22 08:14:01

相關問題