【问题标题】:Change size of UIViewTable to accommodate for AdWhirl Ad更改 UIViewTable 的大小以适应 AdWhirl 广告
【发布时间】:2012-06-28 04:29:53
【问题描述】:

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

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

我也尝试在 scrollViewDidScroll: 选择器中更改它,但没有运气。无论如何我可以改变高度,这样它就不会与底部的广告冲突?谢谢!

【问题讨论】:

  • 你用IB做UITableView吗?

标签: iphone objective-c ios xcode uitableview


【解决方案1】:

使用 UITableViewControllers self.view == self.tableView。在您的情况下这是一个问题,因为您想要的所需效果需要同级视图(两个视图添加到一个公共超级视图),但 self.tableView 没有“超级视图”。

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

我在下面整理了一个简单的示例,可以帮助您入门:

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

【讨论】:

  • 我试过这个,结果我的视图是黑色的,屏幕上什么也没有。
  • 您很可能需要实现自定义表格视图控制器。
  • 我对 Objective-C 很陌生,明天必须为客户推送一个应用程序。有没有简单快捷的方法可以做到这一点?
  • 添加了创建自定义 UITableView 控制器的示例。
【解决方案2】:

解决这个问题的简单方法是为您的 UITableView 使用 .XIB 文件,然后使用 Interface Builder 轻松更改高度。

如果您没有 IB 文件,请浏览此帖子:How do I resize the UITableView's height dynamically?

【讨论】:

  • 我已经尝试过动态编辑它,但它不起作用。我也使用 Interface Builder 对其进行了调整,但没有运气..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多