簡體   English   中英

如何為UITableView tableHeaderView設置寬度,該寬度將比UITableView寬度寬?

[英]How to set width for UITableView tableHeaderView that will be wider than UITableView width?

我在viewDidLoad方法中創建了UITableView ,如下所示:

_tableView = [[UITableView alloc] initWithFrame:CGRectMake(25.0, 0.0, 277.0, 393.0) style:UITableViewStyleGrouped];
mySearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];
[self.tableViewLists setTableHeaderView:mySearchBar];
CGRect newFrame = mySearchBar.frame;
newFrame.size.width = newFrame.size.width + 50.0f;
newFrame.origin.x = newFrame.origin.x - 25.0f;
mySearchBar.frame = newFrame;
[self.tableViewLists setTableHeaderView:mySearchBar];

我想為UITableView tableHeaderView添加UISearchBar ,它將具有以下框架:

CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)

在為UITableView tableHeaderView設置UISearchBar之后,將其框架拉伸為UITableView的寬度。 我可以以某種方式更改UISearchBar的框架嗎?

我認為將UISearchBar放在寬度小於實際UISearchBarUITableView內沒有多大意義。 我認為這是拉伸它的默認行為(在這種情況下,將其收縮)。 您可以看一下這個問題/答案,以獲得一些想法(主要是由於打印屏幕和答案的某些方面)。 我會建議的是UITableView上方的UISearchBar ,就是這樣...

完整的代碼和屏幕截圖

#import "PhotosViewController.h"

@implementation PhotosViewController

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{

 UITableView *table=[[UITableView alloc] initWithFrame:CGRectMake(25.0,0.0, 277.0, 393.0) style:UITableViewStyleGrouped];

 table.delegate=self;
 table.dataSource=self;

 UISearchBar *searchBar=[[UISearchBar alloc] initWithFrame:CGRectMake(10.0f, 0.0f, 257.0f, 44.0f)];

 [table addSubview:searchBar];
 [self.view addSubview:table];

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
 return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}


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

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

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

 UITableViewCell *Cell=[tableView dequeueReusableCellWithIdentifier:cellidentifier];
 if (Cell==nil) {
  Cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier];
 }

 Cell.textLabel.text=@"hello";
 return Cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
 return 44;
}


@end

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM