Uiviewcontroller 如何以编程方式使UITableView变小?
与其把这篇文章和我曾经尝试过但失败过的东西联系起来,我只想保持简单,因为我相信答案可能比我想象的要简单 我的应用程序的主视图上有一个滚动UITableView。我所要做的就是将滚动UITableView的默认位置(或“起点”)向下移动大约194个点,以便为导航栏和其他几个UI元素腾出空间。我该怎么做?以下是我认为分别来自ViewController.h和.m文件的相关方法实现:Uiviewcontroller 如何以编程方式使UITableView变小?,uiviewcontroller,uiscrollview,uitableview,Uiviewcontroller,Uiscrollview,Uitableview,与其把这篇文章和我曾经尝试过但失败过的东西联系起来,我只想保持简单,因为我相信答案可能比我想象的要简单 我的应用程序的主视图上有一个滚动UITableView。我所要做的就是将滚动UITableView的默认位置(或“起点”)向下移动大约194个点,以便为导航栏和其他几个UI元素腾出空间。我该怎么做?以下是我认为分别来自ViewController.h和.m文件的相关方法实现: // MGViewController.h // UITVPractice #import <
// MGViewController.h
// UITVPractice
#import <Foundation/Foundation.h>
@interface ItemsViewController : UITableViewController
-(id) init;
-(id) initWithStyle:(UITableViewStyle)style;
@end
// MGViewController.m
// UITVPractice
#import "ItemsViewController.h"
#import "MGItem.h"
#import "MGItemStore.h"
@implementation ItemsViewController
-(void)viewDidLoad {
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
self.tableView.backgroundView = backgroundImageView;
[super viewDidLoad];
}
-(id) init {
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
/* create 5 random MGItems and place in the MGItemStore */
for (int i = 0; i < 20; i++) {
[[MGItemStore sharedStore] createItem];
}
}
return self;
}
-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [[[MGItemStore sharedStore] allItems] count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
/* Create an instance of UITableViewCell */
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"UITableViewCell"];
}
UIView *backView = [[UIView alloc] initWithFrame:CGRectZero];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;
/* Display custom background image for cell(s) */
cell.backgroundView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"cellBackground.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];
/* Display custom background image for selected cell(s) */
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"cellBackgroundTouched.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];
/* eliminate the white box that bounds the black text. */
[[cell contentView] setBackgroundColor:[UIColor clearColor]];
[[cell backgroundView] setBackgroundColor:[UIColor clearColor]];
[cell setBackgroundColor:[UIColor clearColor]];
/* Set the text of the cell to the description of the item that is at the nth index of items, where n = row this cell will appear in on the tableView */
MGItem *p = [[[MGItemStore sharedStore] allItems] objectAtIndex:[indexPath row]];
[[cell textLabel] setText:[p description]];
[[cell textLabel] setTextColor:[UIColor whiteColor]];
// [[cell textLabel] highlightedTextColor: [UIColor purpleColor]];
return cell;
}
-(id) initWithStyle:(UITableViewStyle)style {
return [self init];
}
@end
//MGViewController.h
//超实践
#进口
@接口项ViewController:UITableViewController
-(id)init;
-(id)initWithStyle:(UITableViewStyle)样式;
@结束
//MGViewController.m
//超实践
#导入“ItemsViewController.h”
#导入“MGItem.h”
#导入“MGItemStore.h”
@实现项viewcontroller
-(无效)viewDidLoad{
UIImageView*backgroundImageView=[[UIImageView alloc]initWithImage:[UIImageName:@“background.png”];
self.tableView.backgroundView=backgroundImageView;
[超级视图下载];
}
-(id)init{
self=[super initWithStyle:UITableViewStyleGrouped];
如果(自我){
/*创建5个随机MGItems并放置在MGItemStore中*/
对于(int i=0;i<20;i++){
[[MGItemStore sharedStore]createItem];
}
}
回归自我;
}
-(NSInteger)表视图:(UITableView*)表视图
节中的行数:(NSInteger)节
{
返回[[[MGItemStore sharedStore]所有项]计数];
}
-(UITableView单元格*)表格视图:(UITableView*)表格视图
cellForRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:@“UITableViewCell”];
/*创建UITableViewCell的实例*/
如果(!单元格){
单元格=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@“UITableViewCell”];
}
UIView*backView=[[UIView alloc]initWithFrame:CGRectZero];
backView.backgroundColor=[UIColor clearColor];
cell.backgroundView=背景视图;
/*显示单元格的自定义背景图像*/
cell.backgroundView=[[UIImageView alloc]initWithImage:[[UIImageName:@“cellbackup.png”]可拉伸图像WithLeftCapWidth:0.0 topCapHeight:5.0]];
/*显示选定单元格的自定义背景图像*/
cell.selectedBackgroundView=[[UIImageView alloc]initWithImage:[[UIImageName:@“cellBackgroundTouched.png”]可拉伸图像,带左CapWidth:0.0 topCapHeight:5.0]];
/*消除限制黑色文本的白色框*/
[[cell contentView]setBackgroundColor:[UIColor clearColor]];
[[cell backgroundView]setBackgroundColor:[UIColor clearColor]];
[cell setBackgroundColor:[UIColor clearColor]];
/*将单元格文本设置为项目第n个索引处的项目描述,其中n=此单元格将显示在tableView中的行*/
MGItem*p=[[MGItemStore sharedStore]allItems]objectAtIndex:[indexPath行]];
[[cell textLabel]setText:[p description]];
[[cell textLabel]setTextColor:[UIColor whiteColor]];
//[[cell textLabel]highlightedTextColor:[UIColor purpleColor]];
返回单元;
}
-(id)initWithStyle:(UITableViewStyle)样式{
返回[自初始化];
}
@结束
我很抱歉,如果这篇文章是一篇“为我做这件事”的文章,但我已经尝试了十几种不同的方法,没有一种有效。我被困在这里大约3天了。谢谢你能提供的帮助
编辑:是的,伊斯梅尔,你说得对。它是UITableViewController的子类。我想我明白你的意思。现在正在做。感谢两位回答。我认为您的控制器是
UITableViewController
的子类?如果是这样的话,那么你必须改变这种情况
为此,将子类更改为UIViewController
,添加UITableViewDelegate
和UITableViewDataSource
协议
然后,添加UITableView*tableView属性并将viewDidLoad方法更改为如下所示:
-(void)viewDidLoad {
[super viewDidLoad]; // [super viewDidLoad]; should go first!!
CGFloat startingPoint = 194.0; // or whatever
CGRect tableRect = self.view.bounds;
tableRect.origin.y = startingPoint;
tableRect.size.height -= startingPoint;
self.tableView = [[UITableView alloc] initWithFrame:tableRect style:UITableViewStyleGrouped]; // or plain, whichever you need
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; // do this if grouped, looks better!
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.tableView];
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
self.tableView.backgroundView = backgroundImageView;
}
编辑:这是有原因的。在正常的
UIViewController
中,self.view
是UIView
,但在UITableViewController
中,self.view
与self.tableView
相同,是UITableView
,这就是为什么您不能更改它的帧。我认为您的控制器是UITableViewController
的子类?如果是这样的话,那么你必须改变这种情况
为此,将子类更改为UIViewController
,添加UITableViewDelegate
和UITableViewDataSource
协议
然后,添加UITableView*tableView属性并将viewDidLoad方法更改为如下所示:
-(void)viewDidLoad {
[super viewDidLoad]; // [super viewDidLoad]; should go first!!
CGFloat startingPoint = 194.0; // or whatever
CGRect tableRect = self.view.bounds;
tableRect.origin.y = startingPoint;
tableRect.size.height -= startingPoint;
self.tableView = [[UITableView alloc] initWithFrame:tableRect style:UITableViewStyleGrouped]; // or plain, whichever you need
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; // do this if grouped, looks better!
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.tableView];
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
self.tableView.backgroundView = backgroundImageView;
}
编辑:这是有原因的。在正常的
UIViewController
中,self.view
是UIView
,但在UITableViewController
中,self.view
与self.tableView
相同,是一个UITableView
,这就是为什么不能更改它的框架。最好的方法是“嵌套”tableview控制器位于另一个UIView中。如果您使用的是故事板,只需添加一个“容器视图”,然后将该视图控制器的类设置为tableview控制器的类。然后,更改容器视图的大小将自动更改表视图的大小。下面是一个示例,其中我在一个视图中嵌套了几个表视图:
最好的方法是将tableview控制器“嵌套”到另一个UIView中。如果您使用的是故事板,只需添加一个“容器视图”,然后将该视图控制器的类设置为tableview控制器的类。然后,更改容器视图