【问题标题】:UISearchDisplayController with custom cell带有自定义单元格的 UISearchDisplayController
【发布时间】:2014-08-13 20:31:56
【问题描述】:

我有一个在storyboard 中设计的自定义单元格,它位于UITableViewController 内,可以很好地与自定义单元格配合使用。

现在,我尝试在 UITableViewControllerUISearchDisplayController 上使用相同的单元格,但它不起作用。

这是我的方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {

    static NSString *CellIdentifier = @"CustomCell";

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier ];
    }

    cell.titleLabel.text = object[@"title"];
    cell.subtitleLabel.text = object[@"subtitle"];

    return cell;
}

它只返回白色的常规单元格,如果我使用默认的cell.textLabel.text,它会显示我的对象。

【问题讨论】:

    标签: ios objective-c uitableview uisearchdisplaycontroller custom-cell


    【解决方案1】:

    试试这个,在storybord上设置标签

    http://i.stack.imgur.com/eVXPE.jpg

    image from AppCoda

    改变你的方法

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
    
    static NSString *CellIdentifier = @"CustomCell";
    
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier ];
    }
    
    //Change this - START
    
    UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];
    UILabel *subtitleLabel = (UILabel *)[cell viewWithTag:101];
    
    titleLabel.text = object[@"title"];
    subtitleLabel.text = object[@"subtitle"];
    
    //Change this - END
    
    return cell;
    }
    

    【讨论】:

      【解决方案2】:

      如果您在故事板上的 tableview 中制作了自定义单元格,我不知道您是否可以在另一个 tableview 中使用它,或者在故事板上的新 tableView 中重新制作自定义单元格,或者制作一个单独的自定义 .xib用于单元格并将它们加载到您的 cellForRow 方法中,例如..

      CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if(!cell)
      {
          [tableView registerNib:[UINib nibWithNibName:@"MyCustomCellNib" bundle:[NSBundle mainBundle] forCellReuseIdentifier:CellIdentifier];
          cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      }
      

      【讨论】:

        【解决方案3】:

        或者更好的是,您可以对现有代码进行一些小的更改,希望它会起作用:

        发件人:

        CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        

        收件人:

        CustomCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        

        或者在 Swift

        发件人:

        var cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier) as! CustomCell
        

        收件人:

        var cell = self.tableView.dequeueReusableCell(withIdentifier: CellIdentifier) as! CustomCell
        

        Credits

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-10-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-07-13
          • 2013-06-01
          • 1970-01-01
          相关资源
          最近更新 更多