你如何以编程方式使 UITableView 变小? [英] How do you make a UITableView smaller programmatically?

查看:27
本文介绍了你如何以编程方式使 UITableView 变小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不会用我尝试过和失败的东西来膨胀和纠结这篇文章,我会保持简单,因为我相信答案可能比我想象的要简单.

Rather than bloat and convolute this post with what I've tried and failed at, I'll just keep it simple, as I'm sure the answer is probably simpler than I think.

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

I have a scrolling UITableView on the main view of my application. All I'm trying to do is move the default position--or "starting point"--of the scrolling UITableView down about 194 points to make room for my navigation bar and a couple other UI elements. How do I do this? Here are what I believe to be the pertinent method implementations from my ViewController .h and .m files, respectively:

//     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 天.感谢您提供的任何帮助.

I apologize if this post comes off as a "do this for me" post, but I've tried about a dozen different things and none of them have worked. Been stuck on this for about 3 days. Thanks for any help you can provide.

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

Yes, Ismael, you're correct. It is a subclass of UITableViewController. I think I understand what you're saying. Working on it now. Thanks to both who answered.

推荐答案

我认为你的控制器是 UITableViewController 的子类?如果是这样,那么您将不得不改变它.

I take it your controller is a subclass of UITableViewController? If that's the case, then you will have to change that.

为此,将子类更改为 UIViewController,添加 UITableViewDelegateUITableViewDataSource 协议.

In order to do this, change the subclass to UIViewController, add the UITableViewDelegate and UITableViewDataSource protocols.

然后,添加一个 UITableView *tableView 属性并将您的 viewDidLoad 方法更改为如下所示:

Then, add a UITableView *tableView property and change your viewDidLoad method to look like this:

-(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.viewself.tableView 一样,是一个 UITableView,这就是为什么你不能改变它的框架.

There's a reason for this. In a normal UIViewController, the self.view is a UIView, but in a UITableViewController, the self.view is the same as self.tableView and is a UITableView, and that's why you can't change the frame of it.

这篇关于你如何以编程方式使 UITableView 变小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆