如果要永久刪除表格單元格,只需撥打deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]
並使用與您的行對應的indexPath。您還需要減少由numberOfRowsInSection:
返回的行數。然而,我正在尋找一種方法來暫時從靜態tableview中「刪除」不需要的靜態tableview單元格,因爲我想要一個特定的行有時在那裏,而其他時間則不是。我也希望能夠從tableview控制器外部按需更新。
我的情況很簡單,因爲它是顯示或隱藏的第一行。你可以推廣以適應你自己的需求。我的tableview控制器是我的數據源委託。
首先,我創建的實現代碼如下控制器的公共方法來更新狀態變量並觸發重新顯示:
- (void)updateSettingsDisplay:(BOOL)hideSetting
{
if (hideSetting == self.hideSetting) {
return; // no need to do anything because it didn't change
}
self.hideSetting = hideSetting;
self.numRows = (hideSetting)? kNumRowsWithSetting : kNumRowsWithoutSetting;
// redisplay only if we're visible
if (!self.viewJustLoaded && (self.navController.visibleViewController == self)) {
[self.tableView reloadData];
}
self.viewJustLoaded = NO;
}
的實現代碼如下控制器的viewDidLoad
樣子:
- (void)viewDidLoad
{
[super viewDidLoad];
// check whether or not to display out-of-coverage tableview cell
self.hideSetting = YES; // initial value; could just as easily be NO
self.viewJustLoaded = YES;
[self updateSettingsDisplay];
}
的tableview中的數據 - 來源代表:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.numRows;
}
and final LY
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// set row to the index of the stored tableView cell that corresponds to the
// cell you want (its location in the static tableview from your storyboard)
NSInteger row = indexPath.row;
if (self.hideSetting) {
// skip drawing setting's tableviewcell (since the setting we're hiding is
// first, we can just increment the row to get the one we want)
++row;
assert(row < kTotalRows); // bounds checking just to convince yourself (remove after testing)
}
// get a new index path since the row field is read-only
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:row inSection:indexPath.section];
// grab the cell from super that corresponds to the cell you want
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:newIndexPath]; // static table
return cell;
}
的訣竅是靜態細胞總是可以在[super tableView:tableView cellForRowAtIndexPath:newIndexPath]
- 所以使用您的靜態的tableview細胞的永久存儲。然後根據需要進行調整的行數,行正確映射在你的tableview委託的cellForRowAtIndexPath:
(即獲得對應於要顯示單元格中的存儲單元格的行)。
的updateSettingsDisplay
方法可以保留它的任何類你的tableview控制器上進行調用。如果當它被稱爲的tableview控制器是不可見的,它只會更新狀態,並等到明年成爲可見的改變顯示的時間。
實際上,這是我發現解決問題的唯一方法,使用方法heightForRowAtIndexPath,爲IndexPath開關並在此函數中進行驗證。如果它是零,我用IBOutlet UITableViewCell引用隱藏單元格,並返回0 ..單元格消失!有20多個細胞時會變得有點令人毛骨悚然,但它效果很好! – Marc
這解決了所有問題https://github.com/xelvenone/StaticDataTableViewController/ –