2013-05-01 28 views
4

我在我的tableview中有56個部分,我將當前選中的部分編號存儲在名爲「activeTimeSlot」的整數變量中。如何使用下面的方法重新加載當前選中的部分。我做它像如何重新加載tableview的特定部分

[self.tblView reloadSections:[NSIndexSet indexSetWithIndex:activeTimeSlot] withRowAnimation:UITableViewRowAnimationAutomatic]; 

,但我觀察到,這樣做的方法是調用的tableview的數據源下面的方法對所有的部分,即其重裝所有的部分。

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

我不知道如何使用NSIndexSet以及如何使用NSIndexSet重新加載超過1節。

+0

檢查我的答案希望它能幫助你。 – Nirav 2013-05-01 07:38:24

+0

你是怎麼知道重裝所有部分的? – 2013-05-01 07:47:46

+0

因爲它的調用numberOfRowsInSection方法的所有部分@ NicolaMiotto我的意思是56次 – 2013-05-01 07:50:01

回答

3

當我從您的意見看到,正在檢查的方法

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

弄清楚,它的重裝各段通話。但是這種方法可能因內部原因而被調用。如果您嘗試記錄哪些行實際上是真實的,您會看到一切正常。把

NSLog(@"Section %d", indexPath.section); 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

,看到剛剛從選定的部分行重載。 PS:忘了說:你的解決方案已經是正確的了。

+0

確定我會檢查 – 2013-05-01 07:52:52

+0

是針對特定部分其調用的cellForRowAtIndexPath只..我太懶了,沒有檢查我假設,如果它的呼叫numberOfRows所有部分,肯定它必須調用cellForRowAtIndexPath所有部分.. :)謝謝反正..) – 2013-05-01 08:04:59

+0

不客氣:) – 2013-05-01 08:05:44

8

如果你想更新多個部分,那麼你可以這樣做。

NSMutableIndexSet *indetsetToUpdate = [[NSMutableIndexSet alloc]init]; 

[indetsetToUpdate addIndex:previousSection]; // [indetsetToUpdate addIndex:<#(NSUInteger)#>] 
// You can add multiple indexes(sections) here. 

[detailTableView reloadSections:indetsetToUpdate withRowAnimation:UITableViewRowAnimationFade]; 

這是用於更新多個部分,它對我來說工作正常。

希望它能幫助你。

+0

好,但爲什麼重新加載一個部分要求數據源的行數在所有部分?像[self.tblView reloadSections:[NSIndexSet indexSetWithIndex:10] withRowAnimation:UITableViewRowAnimationAutomatic];在Section方法中調用numberOFRows 56次。 – 2013-05-01 07:48:32

+0

它可能會讓您在該部分中有很多行。 – Nirav 2013-05-01 07:51:45

+0

這是正常的,如果我重新加載一個部分,它調用所有部分的numberOfRowsInSection方法? – 2013-05-01 07:52:06