1

私は現在、テーブルビューと検索バーを使用してiosでプログラムで検索ビューを作成しようとしています。テーブルビューは期待どおりに機能しますが、検索バーは画面の左側にあり、最後のピクセルが少しだけ表示されます。どこが間違っていたのか教えてください。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    //init array item for table view
    self.timeZoneNames = [NSMutableArray array];
    for(int i = 0; i < 100; i++)
    {
        [self.timeZoneNames addObject:[NSString stringWithFormat:@"a %d", i]];
    }

    //init rectangel that store table view's frame
    UIScreen *mainScreen;
    CGRect rect = CGRectMake(0, 44, [mainScreen applicationFrame].size.width,[mainScreen applicationFrame].size.height);

    //init search bar
    UISearchBar *search = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, [mainScreen applicationFrame].size.width, 44)];

    //init search controller that handle search bar
    //UISearchDisplayController *searchController = [[UISearchDisplayController alloc] initWithSearchBar:search contentsController:self];
    //searchController.delegate = self;
    //searchController.searchResultsDataSource = self;

    //UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [mainScreen applicationFrame].size.width, 44)];
    //[view addSubview:search];

    //init table view, add search bar on top of table view and reload data 
    UITableView *table = [[UITableView alloc] initWithFrame:rect style:UITableViewStylePlain];
    table.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;    
    table.dataSource = self;
    table.delegate = self;
    //table.tableHeaderView = search;
    [table reloadData];

    //set table view to be this controller's view
    //self.view = table;
    self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [mainScreen applicationFrame].size.width, [mainScreen applicationFrame].size.height)];

    self.view.backgroundColor = [UIColor blackColor];
    [self.view addSubview:table];
    [self.view addSubview:search];
}
4

1 に答える 1

2

self.view.boundsの代わりに使用できます[mainScreen applicationFrame]。また、メモリをself.view再度割り当てる必要はありません。その部分を削除できます。

于 2013-01-23T03:24:44.473 に答える