0

アプリで比較的小さなテーブル ビューが必要です。しかし、テーブルビューが画面全体を占めているため、運が悪いようです。

 -(void)viewDidLoad{
 UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 0.0, 20.0, 60.0) style:UITableViewStylePlain];

   self.view = tableView;
 }

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyCellIdentifier = @"MyCellIdentifier";

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyCellIdentifier];

if(cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyCellIdentifier];
}

return cell;
   }

  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
  }
4

3 に答える 3

3

あなたのviewDidLoadメソッドself.view = tableView

[self.view addSubview:tableView];
于 2013-04-05T19:08:44.247 に答える
0

私はしばしばこの問題に遭遇しました。私の経験では、これを処理する最善の方法は、テーブル vc を子 vc として持つコンテナー ビュー コントローラーを作成することです。

それを管理するための簡単なクラスを作成しました。IBでVCをセットアップするだけです。

#import <UIKit/UIKit.h>


@interface ContainerViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIView *contentViewPlaceholderView;
@property (strong, nonatomic) IBOutlet UIViewController *contentViewController;

@end

#import "ContainerViewController.h"

@interface ContainerViewController ()

@property (assign, nonatomic)BOOL isContentVCAdded;

@end

@implementation ContainerViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self setup];
    }

    return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setup];
    }

    return self;
}

- (void)setup
{
    self.isContentVCAdded = NO;
}

- (void)insertAndShowContentVC
{
    [self removeAllChildVCs];
    self.contentViewController.view.frame = self.contentViewPlaceholderView.bounds;
    [self addChildViewController:self.contentViewController];
    [self.contentViewPlaceholderView addSubview:self.contentViewController.view];
    [self.contentViewController didMoveToParentViewController:self];
    self.isContentVCAdded = YES;
}

- (void)removeAllChildVCs
{
    for (UIViewController *vc in self.childViewControllers) {
        [vc willMoveToParentViewController:nil];
        [vc.view removeFromSuperview];
        [vc removeFromParentViewController];
    }
}

- (void)setContentViewController:(UIViewController *)contentVC
{
    _contentViewController = contentVC;
    if (contentVC == nil) {
        [self removeAllChildVCs];
    }
    else {
        [self insertAndShowContentVC];
    }
}

#pragma mark Lifecycle

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (nil != _contentViewController && !self.isContentVCAdded) {
        [self insertAndShowContentVC];
    }
} 

@end

contentViewPlaceholderViewテーブルビューに必要なフレームを正確に持つ (IB またはコードで)を作成するだけです。次に、好きなようにテーブル vc を作成し、それcontentViewControllerContainerViewController

于 2013-04-05T19:17:29.647 に答える