ios - xCode Objective-C : Tapping UITableViewCells without indexPath. 行

标签 ios objective-c iphone xcode uitableview

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath
{
   if (indexPath.row == 0) {
[self performSegueWithIdentifier:@"hello" sender:self];
}
if (indexPath.row == 1) {
    [self performSegueWithIdentifier:@"hello1" sender:self];
}

这是我用来执行 segue 以转到指定的 View Controller 的方法。但是,我已经实现了一个搜索栏。搜索栏过滤某些单元格并重新排序。因此,不同的单元格指向不同的 View Controller 。如果我在搜索栏中搜索“hello1”,它会在表格 View 中将“hello1”显示为第一个,而不会显示“hello”。但由于“hello”设置为 indexPath.row == 0,在应用程序中点击“hello1”将执行“hello”转场,而不是“hello1”转场。我怎样才能解决这个问题?非常感谢任何帮助。

我的搜索条码:

 -(void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
NSLog(@"clicked!");
if ([searchText length] == 0) {
    [_results removeAllObjects];
    [_results addObjectsFromArray:_testphrases];
} else {
    [_results removeAllObjects];
    for (NSString * string in _testphrases) {
        NSRange r = [string rangeOfString:searchText options: NSCaseInsensitiveSearch];
        if (r.location != NSNotFound) {
                [_results addObject:string];
            }
        }

    }
    [tableView reloadData];
}

(_results 是 _resultsArray,_testphrases 是 _masterArray)

新的搜索条码:

-(void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    NSLog(@"clicked!");
    if ([searchText length] == 0) {
        [_results removeAllObjects];
        [_results addObjectsFromArray:_testphrases];
    } else {
        [_results removeAllObjects];
        for (NSDictionary * string in _testphrases) {
            for (NSString *key in string) {
                NSString *value = [string objectForKey:key];
            NSRange r = [value rangeOfString:searchText options: NSCaseInsensitiveSearch];
            if (r.location != NSNotFound) {
                [_results addObject:string];
            }}
        }

    }
    [tableView reloadData];
}

这是搜索发生时的样子: http://imgur.com/a/jpvBi

最佳答案

如果您坚持使用 MVC 模式,您就不会遇到这个问题。您需要做的是为表的数据源准备一个对象数组。这样,您可以引用对象的名称/ID 而不是 indexPath.row。

像这样:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath
{
   Object *object = [_filteredArray objectAtIndex:indexPath.row];
   [self performSegueWithIdentifier:object.segueId sender:self];
}

编辑:很难为您提供帮助,因为您没有提供足够的实现信息/代码。因此,让我添加一个示例来说明如何完成此操作。

  • 创建 2 个可变数组。 masterArray 和 resultsArray。 masterArray 将是我们的 tableview 主要数据源。 resultsArray 是用户搜索的内容。

  • 使用自定义对象或简单的字典填充 masterArray。例如:

    [_masterArray addObject:@{@"title":@"第一项", @"segueID":@"hello"}]; [_masterArray addObject:@{@"title":@"第二项", @"segueID":@"hello1"}];

在这里您指定要将哪个 segue 绑定(bind)到哪个项目。

  • 当用户搜索时,resultArray 应该是 _masterArray 的过滤结果,因此 resultArray 包含 masterArray 的子集,然后显示在表中。

  • 然后在 tableview 的 didSelect delegate 中,您可以获取所选对象及其对应的 segueID 以执行 Segue。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath { NSDictionary *obj = [_resultArray objectAtIndex:indexPath.row]; [self performSegueWithIdentifier:[obj objectForKey:@"segueID"] sender:self];

EDIT2:如何使用数组中的数据填充单元格:

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

    static NSString *cellID = @"yourCellID";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

    }
    if ([resultsArray count]>0) {
    NSDictionary *item = [_resultsArray objectAtIndex:indexPath.row];
    cell.titleLabel.text = [item objectForKey:@"title"];
    } else {
    NSDictionary *item = [_masterArray objectAtIndex:indexPath.row];
    cell.titleLabel.text = [item objectForKey:@"title"];
    }



    return cell;
}

编辑 3:您要做的是直接获取键标题的值,而不是遍历键。这应该有效:

for (NSDictionary *dict in _testphrases) {
            NSString *value = [dict objectForKey:@"title"];
            NSRange r = [value rangeOfString:searchText options: NSCaseInsensitiveSearch];
            if (r.location != NSNotFound) {
                [_results addObject:dict];
            }
        }

关于ios - xCode Objective-C : Tapping UITableViewCells without indexPath. 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44534913/

相关文章:

iphone - 从 ARC 静态库访问 @property 时,ARC 应用程序崩溃

ios - 在 iOS 应用程序中查找图像资源 (XCode 4.2)

ios - 在 Swift 中使用 Objective-C 类时出现 "Cannot Find Initializer"

ios - 实时音频处理 Swift

objective-c - 声明和调用内部方法时出现问题

iphone - 在 iOS 游戏中播放声音最方便的方式是什么?

objective-c - 检查 UIView 是否显示 UIAlertView

iphone - 使用 MacRuby 测试 iPhone 应用程序?

objective-c - .mm 文件上的 NSLog

iphone - 在 UINavigationController 中按下后退按钮时如何调用 viewDidLoad 方法? (iPhone/iPad)