【问题标题】:xCode Objective C: Tapping UITableViewCells without indexPath.rowxCode 目标 C:在没有 indexPath.row 的情况下点击 UITableViewCells
【发布时间】:2017-11-16 00:34:24
【问题描述】:
-(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];
}

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

我的搜索条码:

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

【问题讨论】:

    标签: ios objective-c iphone xcode uitableview


    【解决方案1】:

    如果你坚持 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 将是我们的 tableview 主要数据源。 resultsArray 是用户搜索的时候。

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

      [_masterArray addObject:@{@"title":@"First Item", @"segueID":@"hello"}]; [_masterArray addObject:@{@"title":@"Second Item", @"segueID":@"hello1"}];

    您可以在此处指定要将哪个 segue 绑定到哪个项目。

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

    • 然后在tableview的didSelect委托中,就可以得到选中的对象及其对应的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];
                }
            }
    

    【讨论】:

    • 请详细说明如何将它用于我的特定项目?
    • 我不确定您如何设置您的单元格以及您的数据源是什么样的?
    • -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; } if([results count] > 0 && [results count] > indexPath.row){ cell.textLabel.text = [results objectAtIndex:indexPath.row]; } else { } 返回单元格; }
    • 你能不能也发布所有其他的 tableview 代表代码。以及您如何决定哪些数据进入哪个 segue?
    • 在上面的问题中添加代码(使用编辑)。
    猜你喜欢
    • 2011-04-20
    • 2021-09-07
    • 1970-01-01
    • 2013-10-26
    • 1970-01-01
    • 2021-08-07
    • 1970-01-01
    • 2015-12-16
    • 1970-01-01
    相关资源
    最近更新 更多