tableView:didSelectRowAtIndexPath:调用当前视图的解析器委托 [英] tableView:didSelectRowAtIndexPath: call to current-view Parser delegates

查看:55
本文介绍了tableView:didSelectRowAtIndexPath:调用当前视图的解析器委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些非常有趣的逻辑,基本上我有要检查的数据在显示下一个视图之前..以防万一数据为空,我想弹出视图,如果数据不为空,那么我想加载视图以将其显示在导航堆栈上.

所以在我的tableView:didSelectRowAtIndexPath:方法中,当我做出选择时,获取当前的选择ID号,以便可以将要解析的数据Im的值限制为仅相关值.

这就是我的方法中的代码.

  #pragma标记-表视图委托-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{///...这是用于上下文的..//准备使用子视图VSRViewController * vSRViewController = [[VSRViewController alloc] initWithNibName:@"VSRViewController" bundle:nil];//设置要加载的新视图的后退按钮(这会用后退"覆盖通常的父视图名称)self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@返回"样式:UIBarButtonItemStyleBordered目标:无动作:无];//将标题传递到子视图vSRViewController.title = @"SubModel";//选择的单元格使子视图或要显示的父视图恢复原状..当推或弹出视图时NSPredicate * predicate = [NSPredicate predicateWithFormat:@%K,例如%@",@"MODEL",cell.textLabel.text];filterArray = [parsedDataArrayOfDictionariesfilteredArrayUsingPredicate:predicate];//设置限制字符串,以便在解析subCacheData时仅解析匹配modIdString的值modIdString = [[filterArray valueForKey:@"MODID"] objectAtIndex:0];//限制Mods数据集//这设置要在parserDidEndDocument中输入的语句dataSetToParse = @"ModID";[self startTheParsingProcess:modCacheData];//tempModArra是我从解析器取回的数组,该解析器已将限制字符串应用于该数组如果([tempModArray count] == 0){NSLog(@"POPVIEW");//测试//将此视图弹出到父视图..组织所有必须通过协议和委托发送回的值}否则,如果([[tempModArray count]!= 0){//将所选对象传递给新的视图控制器.[self.navigationController pushViewController:vSRViewController动画:是];//如果未从子视图中删除附件刻度,请检查modIndexPath是否与选定的相同如果(![modIndexPath isEqual:indexPath]){submodIndexPath = nil;}[vSRViewController subModelCachedData:modCacheData indexPath:submodIndexPath dataSetToParse:@"ICSum" modelArray:filterArray modIndexPath:indexPath];//...}}}//... 

此代码已过编辑,以提高可读性,其中还有很多其他内容..因此某些事情可能是错误的..因为我已经编辑了一些名称..但应该没问题..

这就是我的解析器委托中发生的事情.

 -(void)parserDidEndDocument:(NSXMLParser *)parser{//..其他东西在这里.如果(dataSetToParse == @"ModID"){//这适用于NSPredicate * predicate = [NSPredicate predicateWithFormat:@%K,例如%@",@"ModID",modIdString];//modIdString限制已解析的结果NSArray * filteredArray = [parsedDataArrayOfDictionariesfilteredArrayUsingPredicate:predicate];tempModArray =过滤数组;//我该怎么做才能将tempModArray备份到tableView:didSelectRowAtIndexPath:方法..这是我不知所措的地方.}} 

就这样..一切正常,唯一的问题是我无法将temoModArray重新返回到tableView:didSelectRowAtIndexPath:因此,我需要一些解决方案的帮助.

也出于上下文原因,我这样做的原因是,如果tempModArray中没有值,我想将用户发送回父视图,因此当他们进入子视图进行选择时,他们不会看到空的表视图..希望一切都说得通..我期待着我们的答复.

解决方案

我该怎么办才能将tempModArray备份到tableView:didSelectRowAtIndexPath:方法

简短的回答:您不会.

didSelectRow已经完成了他的工作,即通知应用程序用户选择了该行.现在,该应用程序有一些工作要做.即,确定是否要使用数据推送新的视图控制器.因此,不要推送,确定是否有数据,然后弹出;相反,如果没有数据,请不要放在第一位.

在解析器知道它是否有数据的时候,您有很多选择.我假设您的解析器委托不在您的表视图控制器类中.您可以:

  • 发布一个NSNotification,表示您的表视图控制器正在监听,并且监听方法可以在有数据时推送详细信息视图控制器,或者在没有数据时推送无操作.您可以在通知中传递数组.
  • 直接在表视图控制器上调用一个方法以推送详细信息视图控制器,并传入数组(在表视图控制器头中声明一个协议,并使解析器委托调用该方法)
  • 直接从解析器(有点怪)中推送细节视图控制器
  • 使用KVO

imo协议方法是最干净的(耦合松散但命名良好),但是每种方法各有千秋.

I have some pretty interesting logic going, basically I have this data which I want to check over before I display the next view.. just in case the data is empty I want to pop the view, if the data is not empty then I want to load the the view to display it onto the navigational stack.

so in my tableView:didSelectRowAtIndexPath: method, when the selection is made I, get the current selection ID number so I can restrict the values of the data Im going to parse to only related values.

This is what the code looks like inside my method.

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{    
    ///... This stuff is for context.. 

            //Get the subview ready for use
            VSRViewController *vSRViewController = [[VSRViewController alloc] initWithNibName:@"VSRViewController" bundle:nil];
            //Sets the back button for the new view that loads (this overrides the usual parentview name with "Back")
            self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStyleBordered target:nil action:nil];
            //Pass Title over to subview
            vSRViewController.title = @"SubModel";

            //Selected cell gives restult to the subview or o the parent view to be displayed.. when the view is pushed or poped 
            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@",@"MODEL",cell.textLabel.text];
            filterArray = [parsedDataArrayOfDictionaries filteredArrayUsingPredicate:predicate];

            //Sets restriction string so that when subCacheData is parsed only values mathching modIdString will be parsed
            modIdString = [[filterArray valueForKey:@"MODID"] objectAtIndex:0]; //Restricts Mods dataset

            //This sets which if statment to enter in parserDidEndDocument
            dataSetToParse = @"ModID";
            [self startTheParsingProcess:modCacheData];

            //tempModArra is the array I get back from the parser that has had the restriction string applied to it
            if ([tempModArray count] == 0) {
                NSLog(@"POPVIEW"); //testing
                //pop this view to the parent view.. organize all the values that have to be sent back with protocols and delegates
            }else if ([tempModArray count] != 0){

                //Pass the selected object to the new view controller.
                [self.navigationController pushViewController:vSRViewController animated:YES];

                //Check if modIndexPath is same as selected if not remove accessory tick from the subview
                if (![modIndexPath isEqual:indexPath]){
                    submodIndexPath = nil;
                }
                [vSRViewController subModelCachedData:modCacheData indexPath:submodIndexPath dataSetToParse:@"ICSum" modelArray:filterArray modIndexPath:indexPath];

            //.....
            }
            }
    }
    //...

This code has been edited for readability, there is alot of other stuff going on in it.. so some things might be wrong.. as I have edited some of the names.. but its should be fine..

This is whats happening in my parser delegate.

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    //.. other stuff up here.
    if (dataSetToParse == @"ModID") {
        //This applies the 
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@",@"ModID",modIdString]; //modIdString restricts results that have been parsed
        NSArray *filteredArray = [parsedDataArrayOfDictionaries filteredArrayUsingPredicate:predicate];

        tempModArray = filteredArray;

        //what do I do here to get tempModArray back up to tableView:didSelectRowAtIndexPath: method.. this is where I am abit lost.
    }

}

So thats it.. everything works the only issue is that I cannot get my temoModArray back to the tableView:didSelectRowAtIndexPath: so I need some help thinking of a solution.

Also for context the reason I am doing this is that if there are no values in tempModArray I would like to send the user back to the parent view, so they dont see an empty tableview when going to the subview to make a selection.. hope it all make sense.. I look forward to our reply.

解决方案

what do I do here to get tempModArray back up to tableView:didSelectRowAtIndexPath: method

Short answer: you don't.

didSelectRow has already done his job which is to inform the app that the user selected the row. Now the app has some work to do. Namely, figure out if it's going to push a new view controller with data, or not. So don't push, decide if there's data, and then pop; rather, don't push in the first place if there's no data.

At the point where your parser knows whether it has data or not, you have lots of options. I'm assuming your parser delegate is not in your table view controller class. You can:

  • Post an NSNotification that your table view controller is listening for, and the listening method can either push the detail view controller if there's data, or no-op if there's not. You can pass the array in the notification.
  • Call a method directly on your table view controller to push the detail view controller, passing in the array (declare a protocol in your table view controller header and have the parser delegate call into that method)
  • Push the detail view controller directly from the parser (kind of icky)
  • Use KVO

imo the protocol method is the cleanest, (loose coupling but with good naming), but each to their own.

这篇关于tableView:didSelectRowAtIndexPath:调用当前视图的解析器委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆