2012-02-19 30 views
2

我创建了TableViewController子类,并将其与故事板一起使用。我创建了1个动态原型,并在其子类中使用了其标识符值。它工作并显示在故事板中显示的单元格。但是,当我添加searchDisplayController并将TableViewController作为委托和数据源时,它会显示更正的搜索结果,但TableViewCells的格式不再遵循故事板原型。我在下面使用的代码。我怎样才能让它遵循原型?如何使用故事板动态原型TableViewCell格式化SearchDisplayController结果

- (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; 
} 

回答

1

您可以通过对UITableViewCell进行子分类来制作自定义单元格。然后,您必须在layoutSubviews中配置单元格的外观。

这并不能解决您想要在Storyboard中设计单元格的问题。我认为将UISearchControl整合到Storyboard中仍然有点笨拙,并且没有更好的解决方案。的

cell = [tableView dequeueReusableCellWithIdentifier:sCellID]; 

+0

我还计划在自定义单元格。但是想看看是否有更简单的方法。感谢您发布的答案。 – simpleCode 2012-02-20 22:57:20

5

使用

cell = [self.tableView dequeueReusableCellWithIdentifier:sCellID]; 

代替然后使用故事板的原型细胞。