更改 UIViewTable 的大小以适应 AdWhirl 广告 [英] Change size of UIViewTable to accommodate for AdWhirl Ad

查看:18
本文介绍了更改 UIViewTable 的大小以适应 AdWhirl 广告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改 UITableView 的大小.我的视图底部有一个广告,当我滚动时,广告也随之滚动.我想知道如何更改 UITableView 的大小,以便无论 UITableView 是否正在滚动,广告都将始终保留在视图的底部.我曾尝试更改 TableView 框架的大小,但这不起作用.

I am trying to change the size of my UITableView. I have an ad on the bottom of my view, and when I scroll, the ad scrolls along with it. I was wondering how I can change the size of the UITableView so the ad will always remain on the bottom of the view regardless of whether the UITableView is being scrolled or not. I have tried changing the size of the frame of the TableView, but this doesn't work.

- (void)viewDidAppear:(BOOL)animated
{
 tableView.frame = CGRectMake()...
}

我也尝试在 scrollViewDidScroll: 选择器中更改它,但没有成功.无论如何我可以更改高度以使其与底部的广告不冲突吗?谢谢!

I also tried changing it in the scrollViewDidScroll: selector, but no luck. Is there anyway I can change the height so it doesn't conflict with my ad on the bottom? Thanks!

推荐答案

With UITableViewControllers self.view == self.tableView.在您的情况下这是一个问题,因为您想要的效果需要兄弟视图(两个视图添加到一个公共超级视图),但 self.tableView 没有超级视图".

With UITableViewControllers self.view == self.tableView. This is a problem in your case because the desired effect you want requires sibling views (two views added to a common superview) but there is no "superview" for self.tableView.

您必须创建一个新的 UIViewController 子类,该子类将 UITableView 和您的广告视图作为两个子视图.你需要处理一些事情,比如为表格视图设置数据源和委托,以及在控制器出现时取消选择表格视图单元格.这需要更多的工作,需要一些小心,但绝对可行.

You have to create a new UIViewController subclass that has a UITableView and your ad view as two subviews. You will need to handle things like setting the data source and delegate for the table view, as well as deselecting table view cells when the controller appears. This is a little more work and requires some care, but is definitely doable.

我在下面汇总了一个快速示例,可以帮助您入门:

I've thrown together a quick example below that will get you started:

// Header
@interface CustomTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
- (id)initWithStyle:(UITableViewStyle)tableViewStyle;

@property (nonatomic, readwrite, retain) UITableView* tableView;
@end

// Source
@interface CustomTableViewController()
@property (nonatomic, readwrite, assign) UITableViewStyle tableViewStyle;
@end

@implementation CustomTableViewController

@synthesize tableView;
@synthesize tableViewStyle = _tableViewStyle;

- (id)initWithStyle:(UITableViewStyle)tableViewStyle {
  if ((self = [super initWithNibName:nil bundle:nil])) {
    _tableViewStyle = tableViewStyle;
  }
  return self;
}

- (void)loadView {
  [super loadView];

  self.tableView = [[UITableView alloc] initWithStyle:self.tableViewStyle];
  self.tableView.autoresizingMask = (UIViewAutoresizingMaskFlexibleWidth
                                     | UIViewAutoresizingMaskFlexibleHeight);
  self.tableView.delegate = self;
  self.tableView.dataSource = self;
  [self.view addSubview:self.tableView];

  // Create your ad view.
  ...

  adView.autoresizingMask = (UIViewAutoresizingMaskFlexibleWidth
                             | UIViewAutoresizingMaskFlexibleTopMargin);
  [self.view addSubview:adView];

  [adView sizeToFit];
  self.tableView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - adView.frame.size.height);
  adView.frame = CGRectMake(0, self.view.bounds.size.height - adView.frame.size.height, self.view.bounds.size.width, adView.frame.size.height);

  [self.tableView reloadData];
}

- (void)viewDidUnload {
  self.tableView = nil;

  [super viewDidUnload];
}

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  NSIndexPath* selectedIndexPath = [self.tableView indexPathForSelectedRow];
  if (nil != selectedIndexPath) {
    [self.tableView deselectRowAtIndexPath:selectedIndexPath animated:animated];
  }
}

@end

这篇关于更改 UIViewTable 的大小以适应 AdWhirl 广告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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