2013-07-08 50 views
0

我有一个动态的UITableView。它会在点击按钮时插入或删除行。TableView内容高度持续增长

-(IBAction)foldingClicked:(id)sender 
{ 
    self.isInfoFolded = !self.isInfoFolded; 
    if(self.isInfoFolded) 
    { 
     //self.mainTable remove rows 

    }else 
    { 
     //self.mainTable insert rows 
    }  
} 

运行时看起来不错。但点击按钮几次后,我的表视图的contentView的高度增长很多,tableView可以滚动到一个很远的地方,根本没有内容在屏幕上。

我怎么能保持我的tableView的内容高度尽可能多的实际需要?

我只是现在尝试每次单击按钮时记录contentSize.height。看起来每次它增长123像素或缩小95像素。我仍然不知道这28个像素是如何保持内容大小的。我的代码一定有问题。但我应该在哪里检查?

编辑 - 我根据@Stavash的推荐检查了我的编码。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(indexPath.section == SECTION_STORE) 
    { 
     if(self.store) 
     { 
      return self.storeView.frame.size.height; 
     }else 
     { 
      return self.noStoreView.frame.size.height; 
     } 

    } 
    else 
    { 
     return 0; 
    } 
    return 0; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
if(indexPath.section == SECTION_STORE) 
{ 
    if(self.store) 
    { 
     cellIdtifier = @"StoreCell"; 
    }else 
    { 
     cellIdtifier = @"No-StoreCell"; 

    } 
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdtifier]; 
    if(!cell) 
    { 
      cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdtifier]; 
      if(self.store) 
      { 
       [cell.contentView addSubview: self.storeView]; 
      }else 
      { 
       [cell.contentView addSubview: self.noStoreView]; 
      } 

     } 
    return cell; 
    } 
    else 
    { 
     return nil; 
    } 
} 

storeViewnoStoreView从不同高度笔尖文件启动,在高度上既灵活。 看起来是因为单元格视图在点击按钮时增长或缩小,因为高度灵活,它们也会增长。 我的问题最终通过使这两个视图修复高度来解决。 :) :) :)

+0

可能有些代码会帮助 –

+0

显示你的TableView代码..? –

回答

0

我猜你是分配一个相对高度的UITableViewCells在heightForRowAtIndexPath而不是分配一个绝对值。请记住,通常细胞被重复使用,所以这肯定会成为你面临的问题。

如果问题与此无关 - 请提供更多代码以帮助我们理解。

+0

这是因为你说的部分。乍一看代码看起来没问题,但最终发现问题在那里。我编辑我的帖子添加细节。非常感谢! –

相关问题