我正在使用故事板,并希望为我的UITableView实现UISearchbar。 UISearchbarController生成一个新的UITableView,我使用以下策略:
if (tableView == self.tableView)
//Populate table view with normal data
else
//Populate table view with search data
第一个问题是处理自定义单元格。我必须在cellForRowAtIndexPath中实例化它们。通常你会从一个nib文件中做到这一点。我如何使用故事板进行此操作? dequeueReusableCellWithIdentifier返回nil。
第二个问题涉及从表格视图到详细视图的segue。 segue在普通表视图中启动,但不在搜索表视图中启动。由于故事板隐藏了所有内容,我不知道如何将segue分配给搜索表视图的单元格。
有人可以帮忙吗?谢谢!
答案 0 :(得分:6)
关于第一个问题我通过将ViewController类型从UITableViewController更改为UIViewController来解决它,然后在其中添加表视图并为表视图添加IBOutlet
@property (strong, nonatomic) IBOutlet UITableView *_tableView;
使用插座将表格单元格出列
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyTableViewCell *cell=[_tableView dequeueReusableCellWithIdentifier:productMasterCellIdentifier];
// .. modify your cell
return cell;
}
答案 1 :(得分:1)
关于第二个问题:不要从单元格链接segue,而是从viewcontroller链接。您需要在故事板中使用不同的名称制作两个segue。然后在选择行时手动调用segue:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]) {
[self performSegueWithIdentifier:@"fromSearchResults" sender:self];
}
else {
[self performSegueWithIdentifier:@"fromAll" sender:self];
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"fromSearchResults"]) {
..........
..........
}
else {
..........
..........
}
}
答案 2 :(得分:0)
所以这就是我所做的:
1)我在initWithStyle:reuseIdentifier:中初始化了自定义单元格,然后在layoutSubviews中实现了布局。所以我仍然不知道如何从故事板加载单元格。
2)我推动了导航堆栈上的视图,而不是使用故事板segues。