开发者

Add a subview in a UITableViewController

I am using a UITableViewController, I have resized the height of the table view and moved it a little down. Now I'd like to add a UIImageView above that table view but the only thing that I can do is add that UIImageView as a subview of the UITtableView of the UITab开发者_如何学PythonleViewController, so that UIImageView is not above the UITableView and the image scrolls with the table view.

How can I accomplish what I'm trying ? Here is the code I've been using :

UIImageView *tabImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"temp.png"]];
[self.view addSubview:tabImageView];

Thanks!


I believe UITableViewController requires that its view be a UITableView.

In the past, I've worked around this by changing my view controller from subclassing UITableViewController to subclassing UIViewController and implementing the UITableViewDataSource and UITableViewDelegate protocols. Then you can have your view be whatever you want, including shifting your UITableView down and adding a UIImageView.

Not sure if this is still the case in 4.2, though.


EDIT (December 16, 2013)

Just want to clarify that this is really not a great idea for many reasons, a bunch of which are listed in the comments. You should really be doing what Shaggy Frog is suggesting, or using View Controller Containment.

Original Answer

You want the UIImageView to be static at the top of the tableView? If that's so, you need to set it as a subview of the UINavigationController, not the UITableView. Easiest way to do this is with [self.view.superview addSubview:tabImageView]. (although as Shaggy Frog said, it's probably best to change from a UITableViewController to a plain UIViewController, and create the UITableView manually)

If you want the UIImageView to be attached to the UITableView, so that when you scroll down, it disappears, you can just set it as the table header with [self.tableView setTableHeaderView:tabImageView]


Take 4 cells in your static UITableView in UITableViewController with height of 44 PX & width of 320PX. Comment all tableView Delegate methods in UITableViewController.m file and put this code.

- (void)viewDidAppear:(BOOL)animated
{
    UIImageView *backgroundView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 170)];
    backgroundView.image=[UIImage imageNamed:@"default-cover-image.jpg"];
    [self.view.superview addSubview:backgroundView];

    [self.tableView setFrame:CGRectMake(0, 170, 320, 960)];
    [[[UIApplication sharedApplication] keyWindow] setBackgroundColor:[UIColor whiteColor]];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];

    if ((indexPath.row)==0)
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"one"];

        UIImageView *backgroundView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        backgroundView.image=[UIImage imageNamed:@"sectionHeader.png"];

        [cell.contentView addSubview:backgroundView];
    }
    if ((indexPath.row)==1)
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"two"];

        UIImageView *backgroundView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        backgroundView.image=[UIImage imageNamed:@"sectionHeader2.png"];

        [cell.contentView addSubview:backgroundView];
    }
    if ((indexPath.row)==2)
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"three"];

        UIImageView *backgroundView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        backgroundView.image=[UIImage imageNamed:@"sectionHeader3.png"];

        [cell.contentView addSubview:backgroundView];
    }
    if ((indexPath.row)==3)
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"four"];

        UIImageView *backgroundView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        backgroundView.image=[UIImage imageNamed:@"sectionHeader.png"];

        [cell.contentView addSubview:backgroundView];
    }

    return cell;
}

Hope, It'll work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜