如何以编程方式缩小UITableView?

时间:2012-11-26 19:26:37

标签: uiviewcontroller uiscrollview uitableview

我坚持认为答案可能比我想象的要简单,而不是用我曾尝试过的和失败的这篇文章臃肿和卷曲,我只是保持简单,因为我确信答案可能比我想象的要简单。

我的应用程序主视图上有一个滚动的UITableView。我所要做的就是将滚动UITableView的默认位置 - 或“起始点”向下移动大约194点,为我的导航栏和其他几个UI元素腾出空间。我该怎么做呢?以下是我认为分别来自我的ViewController .h和.m文件的相关方法实现:

//     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

如果这篇文章作为“为我做这个”的帖子发布,我很抱歉,但我已经尝试了十几个不同的东西,但没有一个有效。被困在这3天左右。感谢您提供的任何帮助。

编辑:是的,伊斯梅尔,你是对的。它是UITableViewController的子类。我想我明白你在说什么。现在正在努力。感谢两位回答。

2 个答案:

答案 0 :(得分:2)

执行此操作的最佳方法是将tableview控制器“嵌套”在另一个UIView中。如果您使用的是故事板,只需添加“容器视图”,然后将该视图控制器的类设置为tableview控制器的类。然后,更改容器视图的大小将自动更改表视图的大小。这是一个例子,我在一个视图中嵌套了几个表视图:

enter image description here

答案 1 :(得分:1)

我认为你的控制器是UITableViewController的子类?如果是这种情况,那么你将不得不改变它。

要执行此操作,请将子类更改为UIViewController,添加UITableViewDelegateUITableViewDataSource协议。

然后,添加一个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.viewUIView,但在UITableViewController中,self.viewself.tableView相同,并且是UITableView {{1}},这就是为什么你无法改变它的框架。

相关问题