分享

UITableView设置为静态的单元格。 是否有可能隐藏的cell编程?

 没原创_去搜索 2015-07-09
1. 您正在寻找这样的解决方案: StaticDataTableViewController 2.0 它可以显示/隐藏/重装任何静态的单元格带或不带动画!
[self cell:self.outletToMyStaticCell1 setHidden:hide]; 
[self cell:self.outletToMyStaticCell2 setHidden:hide]; 
[self reloadDataAnimated:YES];
注意只(reloadDataAnimated:是/否) (不调用[self.tableView reloadData]直接) 
2. 我的解决方案进入了类似的方向,加雷,虽然我做不同的事情。 这里有云: 1。隐藏的单元格 有没有办法直接隐藏的单元格。UITableViewController是数据源,它提供了静态的cell,目前没有任何方法来告诉它“不提供小区x”。 因此,我们必须为我们自己的数据源,它委托给UITableViewController为了得到静cell。 最简单的是子类UITableViewController和override这就需要隐藏单元格时不同的行为。 在最简单的情况下(单节表 CodeGo.net,所有的cell都有高度),这将是这样的:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
 return [super tableView:tableView numberOfRowsInSection:section] - numberOfCellsHidden;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 // Recalculate indexPath based on hidden cells
 indexPath = [self offsetIndexPath:indexPath];
 return [super tableView:tableView cellForRowAtIndexPath:indexPath];
}
- (NSIndexPath*)offsetIndexPath:(NSIndexPath*)indexPath
{
 int offsetSection = indexPath.section; // Also offset section if you intend to hide whole sections
 int numberOfCellsHiddenAbove = ... // Calculate how many cells are hidden above the given indexPath.row
 int offsetRow = indexPath.row + numberOfCellsHiddenAbove;
 return [NSIndexPathindexPathForRow:offsetRow inSection:offsetSection];
}
如果你的表中有多个部分,或cell有不同的高度,你需要重写的原则也适用于这里:你需要抵消indexPath,部分和行委托给超之前。 也请记住,像indexPathdidSelectRowAtIndexPath:将不同的cell,这取决于状态(即,cell中隐藏的数目)。因此,它可能是一个好主意,总是抵消任何indexPath,并与这些值的工作。 2。动画的变化 由于加雷思已经说过的,你会得到大的毛刺,如果你的动画reloadSections:withRowAnimation:方法。 我发现,如果你调用reloadData:事后,动画大为改善(左只有轻微的毛刺)。该表的动画后正确显示。 那么,我做的是:
- (void)changeState
{
  // Change state so cells are hidden/unhidden
  ...
 // Reload all sections
 NSIndexSet* reloadSet = [NSIndexSetindexSetWithIndexesInRange:NSMakeRange(0, [self numberOfSectionsInTableView:tableView])];
 [tableView reloadSections:reloadSet withRowAnimation:UITableViewRowAnimationAutomatic];
 [tableView reloadData];
}

3. 我了,其实隐藏的部分,并且不删除它们的替代。我试过@henning77的做法,但我一直运行到问题时,我改变了静态的UITableView的节数。已经工作得很好,但我主要是想隐瞒的部分,而不是行。我在飞行中删除行,但它是一个,所以我一直在努力,集团的事情,到我需要显示或隐藏部分。下面是我如何隐藏部分的示例: 首先,我声明一个NSMutableArray里物业
@property (nonatomic, strong) NSMutableArray *hiddenSections;
在viewDidLoad中(或您查询您的数据后),你可以添加你想要隐藏的数组部分。
- (void)viewDidLoad
{
 hiddenSections = [NSMutableArray new];
 if(some piece of data is empty){
  // Add index of section that should be hidden
  [self.hiddenSections addObject:[NSNumber numberWithInt:1]];
 }
 ... add as many sections to the array as needed
 [self.tableView reloadData];
}
那么下面的实现代码如下
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
 if([self.hiddenSections containsObject:[NSNumber numberWithInt:section]]){
  return nil;
 }
 return [super tableView:tableView titleForHeaderInSection:section];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
 if([self.hiddenSections containsObject:[NSNumber numberWithInt:indexPath.section]]){
  return 0;
 }
 return [super tableView:tableView heightForRowAtIndexPath:[self offsetIndexPath:indexPath]];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
 if([self.hiddenSections containsObject:[NSNumber numberWithInt:indexPath.section]]){
  [cell setHidden:YES];
 }
}
然后设置页眉和页脚高度为1隐藏部分,您不能设置高度为0。这额外的2像素的空间,但我们可以弥补它的下一个可见标题的高度。
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{
 CGFloat height = [super tableView:tableView heightForHeaderInSection:section];
 if([self.hiddenSections containsObject:[NSNumber numberWithInt:section]]){
  height = 1; // Can't be zero
 }
 else if([self tableView:tableView titleForHeaderInSection:section] == nil){ // Only adjust if title is nil
  // Adjust height for previous hidden sections
  CGFloat adjust = 0;
  for(int i = (section - 1); i >= 0; i--){
   if([self.hiddenSections containsObject:[NSNumber numberWithInt:i]]){
    adjust = adjust + 2;
   }
   else {
    break;
   }
  }
  if(adjust > 0)
  {    
   if(height == -1){
    height = self.tableView.sectionHeaderHeight;
   }
   height = height - adjust;
   if(height < 1){
    height = 1;
   }
  }
 }
 return height;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section 
{ 
 if([self.hiddenSections containsObject:[NSNumber numberWithInt:section]]){
  return 1;
 }
 return [super tableView:tableView heightForFooterInSection:section];
}
然后,如果你有特定的行隐藏您可以在numberOfRowsInSection且行中的cellForRowAtIndexPath返回。在这个例子中我在这里有一节有三排,其中任意三个可能是空的,需要被删除。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 NSInteger rows = [super tableView:tableView numberOfRowsInSection:section];
 if(self.organization != nil){
  if(section == 5){ // Contact
   if([self.organization objectForKey:@"Phone"] == [NSNull null]){  
    rows--;
   }
   if([self.organization objectForKey:@"Email"] == [NSNull null]){  
    rows--;
   }
   if([self.organization objectForKey:@"City"] == [NSNull null]){  
    rows--;
   }
  }
 }
 return rows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
 return [super tableView:tableView cellForRowAtIndexPath:[self offsetIndexPath:indexPath]];
}
使用此offsetIndexPath计算indexPath的行你在哪里有条件删除行。没有必要的,如果你只是躲在节
- (NSIndexPath *)offsetIndexPath:(NSIndexPath*)indexPath
{
 int row = indexPath.row;
 if(self.organization != nil){
  if(indexPath.section == 5){
   // Adjust row to return based on which rows before are hidden
   if(indexPath.row == 0 && [self.organization objectForKey:@"Phone"] == [NSNull null] && [self.organization objectForKey:@"Email"] != [NSNull null]){  
    row++;
   }
   else if(indexPath.row == 0 && [self.organization objectForKey:@"Phone"] == [NSNull null] && [self.organization objectForKey:@"Address"] != [NSNull null]){  
    row = row + 2;
   }
   else if(indexPath.row == 1 && [self.organization objectForKey:@"Phone"] != [NSNull null] && [self.organization objectForKey:@"Email"] == [NSNull null]){  
    row++;
   }
   else if(indexPath.row == 1 && [self.organization objectForKey:@"Phone"] == [NSNull null] && [self.organization objectForKey:@"Email"] != [NSNull null]){  
    row++;
   }
  }
 }
 NSIndexPath *offsetPath = [NSIndexPath indexPathForRow:row inSection:indexPath.section];
 return offsetPath;
}
有很多覆盖,但我喜欢这种方法,它是安装程序的hiddenSections数组,添加到它,它会隐藏在正确的部分。隐藏的行也有点trouble,但有可能。我们不能只设置我们想要隐藏为0,如果我们'一个分组的UITableView的边框将不会得到正确绘制行的高度。 
4. 是的,这是绝对有可能的,虽然我的问题在挣扎,我设法让cell隐藏,一切工作不错,但我目前还不能做出的东西整齐地制作动画。以下是我发现: 我躲在基于对一个ON / OFF开关的优先部分的优先行中的状态行。如果开关处于ON有1排在它下面的部分,否则有2个不同的行。 我有一个叫当开关切换选择器,我设置一个变量来表示我的状态。然后我打电话:
[[self tableView] reloadData];
我重写的实现代码如下:willDisplayCell:forRowAtIndexPath:函数,如果电池是应该被隐藏我这样做:
[cell setHidden:YES];
隐藏的单元格,其内容,但不删除它占用的空间。 要删除空间,覆盖实现代码如下:heightForRowAtIndexPath:函数,返回0,表示不应该被隐藏的行。 您还需要重写实现代码如下:numberOfRowsInSection:与返回行的那部分数量。你所要做的奇怪在这里,所以,如果你的表是一个combinations的样式出现在正确的cell圆角。在我的静态表有整套cell的部分,所以包含该选项的优先个单元格,然后1cell为ON状态和2个以上的cell为OFF状态的选项,一共有4个cell。当选项为ON时,我不得不返回4,这包括隐藏的选项,使得所显示的最后一个选项有一个圆形的盒子。当选项是关闭的,不显示最后两个选项,所以我返回2。这一切都感觉沉闷。很抱歉,如果这不是很清楚,它的棘手来形容。只是到安装,这是在IB表款的建设: 行0:选项有ON / OFF开关 第1行:当选项为ON时显示 第2行:当选项为OFF显示 第3行:当选项为OFF显示 所以当选项设置为ON的表报告两行分别是: 行0:选项有ON / OFF开关 第1行:当选项为ON时显示 如果选项是关闭的表报告四行分别是: 行0:选项有ON / OFF开关 第1行:当选项为ON时显示 第2行:当选项为OFF显示 第3行:当选项为OFF显示 这种方法并不觉得正确的有以下几个原因,它只是据我已经得到了我那么远,那么请知道如果你找到一个更好的办法。我观察到的问题,到目前为止有: 这感觉不对被告诉该表的行数是不同的东西大概是包含在基础数据中。 我似乎无法以动画的变化。我已经实现代码如下:reloadSections:withRowAnimation:不是reloadData,结果似乎并没有什么意义,我仍然试图得到这个工作。目前什么似乎是发生了的tableView不更新正确的行这么一保持隐藏状态,应该在优先行下显示和空隙离开了。我认为这可能与有关基础数据的优先点。 希望将能够建议或者如何与动画延长,但也许这将让你开始。我的道歉缺乏超链接的函数,我把他们但他们的垃圾邮件过滤器被拒绝了我是一个相当 
5. 我一直在争吵与UITableView的几个小时。如果你不关心的动画,只是想隐藏静态cell,这个工作
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
 UITableViewCell* cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
 if(cell == cellYouWantToHide)
  return 0; //set the hidden cell's height to 0
 else
  return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
请记住,你躲在需要有ClipToBounds=YEScell。否则,该文本将堆积在UITableView中。 
6. 我发现在静态表动画躲在cell的溶液。
// Class for wrapping Objective-C block
typedef BOOL (^HidableCellVisibilityFunctor)();
@interface BlockExecutor : NSObject
@property (strong,nonatomic) HidableCellVisibilityFunctor block;
+ (BlockExecutor*)executorWithBlock:(HidableCellVisibilityFunctor)block;
@end
@implementation BlockExecutor
@synthesize block = _block;
+ (BlockExecutor*)executorWithBlock:(HidableCellVisibilityFunctor)block
{
 BlockExecutor * executor = [[BlockExecutor alloc] init];
 executor.block = block;
 return executor;
}
@end
只有一个额外的字典需要:
@interface MyTableViewController ()
@property (nonatomic) NSMutableDictionary * hidableCellsDict;
@property (weak, nonatomic) IBOutlet UISwitch * birthdaySwitch;
@end
再看看MyTableViewController的。我们需要转换indexPath有形和无形指标之间...
- (NSIndexPath*)recoverIndexPath:(NSIndexPath *)indexPath
{
 int rowDelta = 0;
 for (NSIndexPath * ip in [[self.hidableCellsDict allKeys] sortedArrayUsingSelector:@selector(compare:)])
 {
  BlockExecutor * executor = [self.hidableCellsDict objectForKey:ip];
  if (ip.section == indexPath.section
   && ip.row <= indexPath.row + rowDelta
   && !executor.block())
  {
   rowDelta++;
  }
 }
 return [NSIndexPath indexPathForRow:indexPath.row+rowDelta inSection:indexPath.section];
}
- (NSIndexPath*)mapToNewIndexPath:(NSIndexPath *)indexPath
{
 int rowDelta = 0;
 for (NSIndexPath * ip in [[self.hidableCellsDict allKeys] sortedArrayUsingSelector:@selector(compare:)])
 {
  BlockExecutor * executor = [self.hidableCellsDict objectForKey:ip];
  if (ip.section == indexPath.section
   && ip.row < indexPath.row - rowDelta
   && !executor.block())
  {
   rowDelta++;
  }
 }
 return [NSIndexPath indexPathForRow:indexPath.row-rowDelta inSection:indexPath.section];
}
一个IBAction上UISwitch值变化:
- (IBAction)birthdaySwitchChanged:(id)sender
{
 NSIndexPath * indexPath = [self mapToNewIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]];
 if (self.birthdaySwitch.on)
  [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
 else
  [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
UITableViewDataSource和
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 int numberOfRows = [super tableView:tableView numberOfRowsInSection:section];
 for (NSIndexPath * indexPath in [self.hidableCellsDict allKeys])
  if (indexPath.section == section)
  {
   BlockExecutor * executor = [self.hidableCellsDict objectForKey:indexPath];
   numberOfRows -= (executor.block()?0:1);
  }
 return numberOfRows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 indexPath = [self recoverIndexPath:indexPath];
 return [super tableView:tableView cellForRowAtIndexPath:indexPath];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
 indexPath = [self recoverIndexPath:indexPath];
 return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
- (void)viewDidLoad
{
 [super viewDidLoad];
 // initializing dictionary
 self.hidableCellsDict = [NSMutableDictionary dictionary];
 [self.hidableCellsDict setObject:[BlockExecutor executorWithBlock:^(){return self.birthdaySwitch.on;}] forKey:[NSIndexPath indexPathForRow:1 inSection:1]];
}
- (void)viewDidUnload
{
 [self setBirthdaySwitch:nil];
 [super viewDidUnload];
}
@end

7. 最好的办法是如下描述的博客 
8. 对于最简单的情况下,当你隐藏单元格在表视图的底部,你可以实现代码如下的contentInset后您隐藏单元格:
- (void)adjustBottomInsetForHiddenSections:(NSInteger)numberOfHiddenSections
{
 CGFloat bottomInset = numberOfHiddenSections * 44.0; // or any other 'magic number
 self.tableView.contentInset = UIEdgeInsetsMake(self.tableView.contentInset.top, self.tableView.contentInset.left, -bottomInset, self.tableView.contentInset.right);
}


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约