设置我的表视图的边界不起作用(xcode 4.2,iOS 5.0)
我想设置我的表视图的边界(在UITableViewController中),使其宽度小于屏幕宽度(这样可以在表视图后面看到背景图像)。我在viewDidLoad方法中使用以下代码行:
I would like to set the bounds for my table view (in a UITableViewController) so that it has a smaller width than the screen width (so that a background image can be seen behind the table view). I use the following line of code in my viewDidLoad method:
[[self tableView] setBounds:CGRectMake(10,44, 300, 460)];
但是,此行无效,并且tableview仍占用页面的整个宽度。
However, this line is not working and the tableview still takes up the whole width of the page.
如何才能使这项工作?
此外,只要我在定制表的主题视图,我还想更改单元格中文本的字体。我该怎么做呢?谢谢。
Also, as long as I'm on the subject of customizing table views, I would also like to change the font of the text in the cells. How would I go about doing that? Thank you.
A UITableViewController
的 view
property是 UITableView
。所以你不能缩小它的视图
来揭示它背后的东西,因为它背后没有任何东西(至少就其而言)。
A UITableViewController
's view
property is a UITableView
. So you cannot shrink it's view
to reveal what's behind, since there is nothing (At least as far as it's concerned) behind it.
有一个相对简单的解决方案。我假设,根据您的代码示例,您的控制器上有一个名为 tableView
的 UITableView
属性,如果不是它的创建是必要的。
There is a relatively easy solution. I'm assuming, based on your code sample that you have a UITableView
property on your controller named "tableView
", if not it's creation would be necessary.
1)将您的视图控制器更改为 UIViewController
子类而不是 UITableViewController
子类。通过使用 YourClass:UIViewController $替换
YourClass:UITableViewController
,轻松完成 .h
c $ c>。
1) Change your view controller to be a UIViewController
subclass instead of a UITableViewController
subclass. Easily done in the .h
by replacing YourClass:UITableViewController
with YourClass:UIViewController
.
2)在 .xib
文件中拖出一个新的 UIView
到画布(不在tableView上)。
2) In the .xib
file drag out a new UIView
to the canvas (not on the tableView).
3)拖动tableView作为视图的子视图,如图所示:
3) Drag the tableView to be a subview of the view, as shown:
4)将文件所有者的视图属性连接为刚添加的UIView。
4) Connect the File's Owner's view property to be the UIView you just added.
5)检查您的连接是否如下所示:
5) Check that your connections look something like this:
通过这些更改,您现在可以设置框架小于视图的大小。你有两种选择:
1)使用类似代码的代码:
With these changes you can now set your frame to be smaller than your view's size. You have two choices how to do this: 1) Use code like this code:
-(void)viewDidLoad{
[super viewDidLoad];
// set background red to see change
self.view.backgroundColor = [UIColor redColor];
CGFloat sideMargin = 10;
CGFloat topBottomMargin = 44;
CGFloat originX = sideMargin;
// compute width based on view size
CGFloat sizeWidth = (self.view.bounds.size.width - (sideMargin * 2));
CGFloat originY = topBottomMargin;
// compute height based on view size
CGFloat sizeHeight = (self.view.bounds.size.height - (topBottomMargin * 2));
// set tableView frame
self.tableView.frame = CGRectMake(originX, originY, sizeWidth, sizeHeight);
}
2)直接更改界面生成器中的设置。类似于:
2) Change the setting in interface builder directly. Something like: