2014-05-16 23 views
1

我想在更新後插入一些部分,但我有相同的元素。我該怎麼做?我想插入最後一個元素之後的元素。但有錯誤。insertSections in tableView

我的代碼

 [self.tableView beginUpdates]; 
    for (WallPost *wp in news) { 
     if (!_tableDataSource.count || ![_tableDataSource containsObject:wp]) { 
      [_tableDataSource insertObject:wp atIndex:_tableDataSource.count]; 
      [self.tableView insertSections: 
       [NSIndexSet indexSetWithIndex: 
       _tableDataSource.count-1] 
       withRowAnimation:UITableViewRowAnimationBottom]; 
     } 
    } 
    [self.tableView endUpdates]; 
+0

你得到什麼錯誤? – RaffAl

回答

1

我不知道你有什麼確切的錯誤,但你在tableViewfor週期插入section。相反,你應該對象添加到陣列,然後使用一組指數由法indexSetWithIndexesInRange重新加載部分中tbaleView

int count = 0; 
for (WallPost *wp in news) { 
    if (!_tableDataSource.count || ![_tableDataSource containsObject:wp]) { 
     [_tableDataSource addObject:wp]; 
     count++; 
    } 
} 

if(count > 0) { 
    [self.tableView beginUpdates]; 
    [self.tableView insertSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(_tableDataSource.count-count, count) withRowAnimation:UITableViewRowAnimationBottom]; 
    [self.tableView endUpdates]; 
} 

只是一個報告:

你不」噸需要使用:

[_tableDataSource insertObject:wp atIndex:_tableDataSource.count]; 

,因爲你每次加在end..so對象喜歡:

[_tableDataSource addObject:wp]; 
+0

當您添加其他部分時,您應該使用[self.tableView reloadSections:[Range] with Animation:Animation]重新加載插入變體。 –