如何使用故事板动态原型 TableViewCell 来格式化 SearchDisplayController 结果
我创建了 TableViewController 子类并将其与故事板一起使用。我创建了 1 个动态原型并在子类中使用了它的标识符值。它工作并显示故事板中显示的单元格。但是,当我添加 searchDisplayController 并使 TableViewController 作为委托和数据源时,它显示正确的搜索结果,但 TableViewCells 的格式不再遵循故事板原型。我在下面使用的代码。我怎样才能让它遵循原型?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Contact Cell";
static NSString *sCellID = @"Contact Cell";
UITableViewCell *cell;
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell = [tableView dequeueReusableCellWithIdentifier:sCellID];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:sCellID];
}
}
else
{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
}
// ask NSFetchedResultsController for the NSMO at the row in question
Contact *contact = [self.fetchedResultsController objectAtIndexPath:indexPath];
// Then configure the cell using it ...
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.imageView.image = [UIImage imageNamed:@"1234_profile.png"];
cell.textLabel.text = [contact.lastName stringByAppendingFormat:@", %@",contact.firstName];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@, %@", contact.occupation, contact.companyName];
return cell;
}
I created TableViewController subclass and used it with a storyboard. I created 1 Dynamic Prototype and used its Identifier value in the subclass. It works and displays the cells as it shows in the storyboard. But when I added the searchDisplayController and made the TableViewController the delegate and datasource, it displays the corrct search results, but the format of the TableViewCells no longer followed the storyboard prototype. Code I used below. How can I make it follow the prototype?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Contact Cell";
static NSString *sCellID = @"Contact Cell";
UITableViewCell *cell;
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell = [tableView dequeueReusableCellWithIdentifier:sCellID];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:sCellID];
}
}
else
{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
}
// ask NSFetchedResultsController for the NSMO at the row in question
Contact *contact = [self.fetchedResultsController objectAtIndexPath:indexPath];
// Then configure the cell using it ...
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.imageView.image = [UIImage imageNamed:@"1234_profile.png"];
cell.textLabel.text = [contact.lastName stringByAppendingFormat:@", %@",contact.firstName];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@, %@", contact.occupation, contact.companyName];
return cell;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
而不是
然后使用故事板的原型单元。
use
instead of
Then a prototype cell of storyboard is used.
您可以通过子类化 UITableViewCell 来创建自定义单元格。然后您必须在layoutSubviews 中配置单元格的外观。
这并不能解决您想要在故事板中设计单元格的问题。我认为将 UISearchControl 集成到 Storyboards 中仍然有点笨拙,并且没有更好的解决方案。
You can make a custom cell by sub classing UITableViewCell. Then you have to configure the appearance of the cell in layoutSubviews.
This does not solve your problem that you want to design your cells in Storyboards. I think that the integration of the UISearchControl into Storyboards is still a little clumsy and that there is no better solution.