2012-07-04 192 views
0

viewDidLoad方法是這樣創造UITableView如何設置UITableView tableHeaderView的寬度將比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]; 

我要添加UISearchBarUITableViewtableHeaderView,將有這樣的框架:我把我的UISearchBarUITableView tableHeaderView

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

,這是框架拉伸爲UITableView的寬度。我可以以某種方式更改UISearchBar的框架?

+0

你在哪裏設置tableHeaderView的寬度,並在你正在添加搜索欄。你可以添加代碼嗎? – rishi

回答

0

UISearchBar放在UITableView內沒有什麼意義,寬度小於實際的UISearchBar。我認爲這將是默認的行爲來拉伸它(在這種情況下縮小它)。你可以看看這個question/answe r得到一些想法(主要是因爲打印屏幕和一些答案)。我會建議,將是UITableView以上UISearchBar,這就是它......

0

完整的代碼與截圖

#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 *[email protected]"cellidentifier"; 

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

[email protected]"hello"; 
return Cell; 
} 

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


@end 

enter image description here

+0

但是當我滾動'UITableView'時,我需要在'UINavigationController'下隱藏'UISearchBar' ... – ZigDanis

+0

好的,等我看到了 – Warewolf

+0

在這個代碼中,當你滾動表格視圖時,如果導航欄是隱藏的,那裏 – Warewolf