Добавить подпредставление в UITableViewController - PullRequest
8 голосов
/ 10 декабря 2010

Я использую UITableViewController, я изменил высоту представления таблицы и немного переместил ее.Теперь я хотел бы добавить UIImageView над этим табличным представлением, но единственное, что я могу сделать, это добавить этот UIImageView в качестве подпредставления UITtableView UITableViewController, чтобы UIImageView не был выше UITableView, и изображение прокручивается вместе с таблицей.view.

Как мне добиться того, что я пытаюсь?Вот код, который я использовал:

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

Спасибо!

Ответы [ 3 ]

18 голосов
/ 10 декабря 2010

Я считаю, что UITableViewController требует, чтобы view было UITableView.

В прошлом я работал над этим, меняя контроллер представления с подклассов UITableViewController на подклассы UIViewController и реализуя протоколы UITableViewDataSource и UITableViewDelegate. Тогда вы можете сделать так, чтобы ваш view был любым, включая смещение UITableView вниз и добавление UIImageView.

.

Не уверен, что это все еще так в 4.2.

9 голосов
/ 10 декабря 2010

РЕДАКТИРОВАТЬ (16 декабря 2013 г.)

Просто хочу уточнить, что это действительно не очень хорошая идея по многим причинам, многие из которых перечислены в комментариях. Вы действительно должны делать то, что предлагает Shaggy Frog, или использовать View Controller Containment.

Оригинальный ответ

Вы хотите, чтобы UIImageView был статическим в верхней части tableView? Если это так, вам нужно установить его как подпредставление UINavigationController, а не UITableView. Самый простой способ сделать это с помощью [self.view.superview addSubview:tabImageView]. (хотя, как сказал Shaggy Frog, вероятно, лучше всего перейти с UITableViewController на обычный UIViewController и создать UITableView вручную)

Если вы хотите, чтобы UIImageView был присоединен к UITableView, чтобы при прокрутке вниз он исчезал, вы можете просто установить его в качестве заголовка таблицы с помощью [self.tableView setTableHeaderView:tabImageView]

0 голосов
/ 08 февраля 2015

Возьмите 4 ячейки в вашем статическом UITableView в UITableViewController с высотой 44 PX и шириной 320PX. Прокомментируйте все tableView Делегируйте методы в UITableViewController.m файле и поместите этот код.

- (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;
}

Надеюсь, это сработает.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...