5

我实际上创建了一个UIScrollView,它内部带有一个UITableView接口构建器。我把我的UIScrollView与内容大小:UIscrollView中的UITableView的内容大小问题

scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height); 

,我把我的文件的所有者欣赏到UIScrollView

我创建了一个IBOutletUITableView,我把它在界面生成器。

加载视图时。它会显示我的UIScrollView的正确内容大小。但它不显示我的UITableView的正确内容大小。此外,当我更改我的UIScrollView的内容大小时,它会更改我的UITableView的内容大小,就好像两个内容大小都是相关的。

任何人都知道如何保持我在界面生成器中设置的表视图的内容大小?

回答

12

A UITableViewUIScrollView。在UIScrollView中嵌入UITableView将很有用。

假设你确实需要这个,你需要在某个地方做下面的事情。可能在UIViewController的方法中,例如viewDidAppear:或您填充表格视图后的某个位置。

// Set the size of your table to be the size of it's contents 
// (since the outer scroll view will handle the scrolling). 
CGRect tableFrame = tableView.frame; 
tableFrame.size.height = tableView.contentSize.height; 
tableFrame.size.width = tableView.contentSize.width; // if you would allow horiz scrolling 
tableView.frame = tableFrame; 

// Set the content size of your scroll view to be the content size of your 
// table view + whatever else you have in the scroll view. 
// For the purposes of this example, I'm assuming the table view is in there alone. 
scrollView.contentSize = tableView.contentSize; 
+0

它仍然没有工作...其实当我NSLog tableView.frame它显示我(空)。我不明白... – kschaeffler 2012-01-09 15:08:23

+0

其实,我的文件的所有者是UIScrollView和UITableView的代表,它是一个scrollView。这是个问题吗? – kschaeffler 2012-01-09 15:11:23

+0

我应该更加明确。 'tableView'和'scrollView'应该是与这些项目关联的IBOutlets。 – DBD 2012-01-09 16:02:06

0

如果仍有问题,请尝试此操作而不需要scrollView。

与DBD的例子了一会儿玩弄后,我发现,在框架和contentSize似乎不能够一起设置,如:

self.tableView.contentSize = CGSizeMake(320, scrollHeight); 
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,44,self.tableView.contentSize.width,self.tableView.contentSize.height); 

的尝试是设置的一个最大高度但如果存在大量单元格,请保留滚动所有内容的功能。这种破解似乎工作得很好,如果需要可以使用更多条件进行修改:

int cellCount = [YOURARRAY count]; 
CGFloat scrollHeight = cellCount*44+44;//for a cell with height of 44 and adding 44 if you have a toolbar at the bottom 

self.tableView.contentSize = CGSizeMake(320, scrollHeight);//you can change the width and or let the .xib control the autoresizing 

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && cellCount < 14) 
{ 
    //here's were you can add more conditions if you're in landscape or portrait, but this handles 14 cells with a heightForHeaderInSection of 46 in landscape 
    self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,44,self.tableView.contentSize.width,scrollHeight); 
} 
else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && cellCount < 7) 
{ 
    //same as above but it's for the iPhone in portrait 
    self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,44,self.tableView.contentSize.width,scrollHeight); 
} 

这是有效的。您可能需要在.xib或代码中调整autoresizingMask,但这种破解是我发现的唯一解决方案,它处理了我的情况下所有不同的变量。

1

实施例来动态地与滚动型沿着管理的TableView高度,工作在夫特:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     // Custom Cell 
     let cell:TblPortCell = self.tableList.dequeueReusableCellWithIdentifier("cell") as! TblPortCell 

     //tableList is the OutLet for the table 
     tableList.sizeToFit() 
     // Set the size of the table to be the size of it's contents 
     // (since the outer scroll view will handle the scrolling). 
     let height = tableList.frame.size.height 
     let pos = tableList.frame.origin.y 
     let sizeOfContent = height + pos + 130 
     //scrollView is the Outlet for the Scroll view 
     scrollView.contentSize.height = sizeOfContent 
     scrollView.sizeToFit() 

     cell.selectionStyle = UITableViewCellSelectionStyle.None 
     return cell 
    } 

提及:下面方法进行的多重

0

呼叫倍。每次打电话都会有更多的细胞。

CGRect tableFrame = tableView.frame; 
tableFrame.size.height = tableView.contentSize.height; 
tableView.frame = tableFrame; 

scrollView.contentSize = tableView.contentSize;