我正在尝试实现 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/