2012-11-09 37 views
1

我有一个带有搜索栏的TableView。但我想添加另一个视图(可以说是一个标签)到TableView中(所以它与tableview一起滚动)。例如,如果您打开iPhone手机通讯录应用程序,在所有号码下,您可以看到一个名为“我的号码:”的标签。它必须与搜索栏处于同一层级。我试图在搜索栏上方添加一个新标签,但故事板不允许我这样做。使用搜索栏自定义UITableView

Document Outline Screenshot for the ViewController

在这里,如果我拖放一个UILabel此层次结构,要么替换搜索栏上方,或标签原型细胞内下降。没有其他方式一次显示搜索栏和标签。

有没有办法做到这一点?

回答

1

创建视图控制器的视图之外的单独视图(或里面,也不要紧,你甚至可以以编程方式创建此),并将其链接到一个IBOutlet myCustomView。 创建IBOutlet搜索栏并链接您的UISearchBar

in viewDidLoad;你可以使用 [searchBar addSubView:myCustomView]; 并在搜索栏上方添加您的自定义视图。

您可以显示/隐藏(myCustomView.hidden = YES/NO)自定义视图,或者在需要时将其从超级视图中添加或删除。

前。

- (IBAction)didTapShowCustomHeaderButton { 
    myCustomView.hidden = NO; 
} 

- (IBAction)didTapHideCustomHeaderButton { 
    myCustomView.hidden = YES; 
} 
0

添加下面的两名代表在代码

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
    { 

    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 30)] autorelease]; 
     label.text = @"My Label"; 
    label.backgroundColor = [UIColor clearColor]; 

     return label; 

    } 

-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section{ 
     return 30 // this height should be equivalent to the myLabel height u declared in headerView above 
     } 
+0

嗨,感谢您的回复。但是我需要这个标签在滚动表格时使用搜索栏离开屏幕。 – sleepwalkerfx

+0

雅在哪里想要它..我假设你需要它作为tableview的一部分 – AppleDelegate

+0

我需要在运行时隐藏搜索栏,并显示另一个UIcontrol当搜索栏被隐藏。为了做到这一点,我认为我必须将其他UIcontrol添加到搜索栏精确定位的位置(分层) – sleepwalkerfx