我有一个动态的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;
}
}
的storeView
和noStoreView
从不同高度笔尖文件启动,在高度上既灵活。 看起来是因为单元格视图在点击按钮时增长或缩小,因为高度灵活,它们也会增长。 我的问题最终通过使这两个视图修复高度来解决。 :) :) :)
可能有些代码会帮助 –
显示你的TableView代码..? –