iphone - SearchDisplayControler、UILabel选择后堆叠

标签 iphone objective-c ios

我正在尝试实现 searchDisplayController,但我遇到了问题。一旦我过滤了单元格并选择了剩余的单元格,我得到了一堆奇怪的文本:

http://postimage.org/image/4fswe8t8n/

这就像把我所有的手机都合二为一。

所以这是我的 cellForRowAtIndexPath 方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MFriendCell";

UITableViewCell *cell = [tableView
                         dequeueReusableCellWithIdentifier:CellIdentifier];

if(cell == nil)  {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

if(tableView == self.tableView) {
    UILabel *nameLabel = (UILabel *)[cell viewWithTag:201];
    nameLabel.text = [self.allItems objectAtIndex:indexPath.row];
}
else if(tableView == self.searchDisplayController.searchResultsTableView) {
    UILabel *labelName = [ [UILabel alloc ] initWithFrame:CGRectMake(50, 0.0, 150.0, 43.0) ];
    labelName.text = [self.searchResults objectAtIndex:indexPath.row];

    [cell addSubview:labelName];
}
return cell;
}

还有我的过滤器:

- (void)filterContentForSearchText:(NSString*)searchText
                         scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate
                                predicateWithFormat:@"SELF contains[cd] %@",
                                searchText];

self.searchResults = [self.allItems filteredArrayUsingPredicate:resultPredicate];
}

如果你需要什么就问:)

最佳答案

您可以像这样在 tableView 中制作标签

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MFriendCell";

    UITableViewCell *cell = [tableView
                             dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    UILabel *nameLabel = (UILabel *)[cell viewWithTag:201];

    if(tableView == self.tableView)
    {
       nameLabel.text = [self.allItems objectAtIndex:indexPath.row]; 
    }

    else if(tableView == self.searchDisplayController.searchResultsTableView)
    {
        labelName.text = [self.searchResults objectAtIndex:indexPath.row];
    }
    return cell;
}

关于iphone - SearchDisplayControler、UILabel选择后堆叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13743685/

相关文章:

ios - 取消选中 Swift4 中的 TableView 所有 TableView 选择

iphone - 将两个 CoreData 关系(具有逆关系)分配给同一实体

iphone - 如何在iPad App中显示这个?

ios - 从 iOS 上传图片到 PHP 服务器

iphone - MPMoviePlayerViewController 视频时长

ios - Iphone 模拟器屏幕键盘问题

ios - "Hijack"OSX 和理想情况下 iOS 上的系统范围 http 链接并打开 native 应用程序

iphone - 通过NSNotification设置时,UIImageView.image需要花费几秒钟的时间才能显示

ios - 从 AppDelegate 发送 NSNotification

javascript - 通过 UIWebView 在 JavaScript 中处理 Window.close - Obj C