如果要永久删除表格单元格,只需拨打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/ –