代码之家  ›  专栏  ›  技术社区  ›  Oh Danny Boy

目标C:rowatindexpath的范围问题单元

  •  0
  • Oh Danny Boy  · 技术社区  · 14 年前

    更新:这似乎是“结果”范围的问题。TitleForHeaderInSection似乎不知道“结果”数组的内容。一旦我找到一个解决方案,我会贴出来以防万一有人碰到同样的问题。

    如何将cellForRowatindexpath中的每一行设置为由提取请求填充的数组的结果?(按下按钮时发出的提取请求。)

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        //  ... set up cell code here ...
        cell.textLabel.text = [results objectAtIndex:indexPath valueForKey:@"name"];
    
    }
    
    warning: 'NSArray' may not respond to '-objectAtIndexPath:'
    

    编辑:

    - (NSArray *)SearchDatabaseForText:(NSString *)passdTextToSearchFor{
        NSManagedObject *searchObj;
        XYZAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    
        NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;
        NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains [cd] %@", passdTextToSearchFor]; 
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Entry" inManagedObjectContext:managedObjectContext]; 
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO]; 
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];   
    
        [request setSortDescriptors:sortDescriptors];
        [request setEntity: entity]; 
        [request setPredicate: predicate]; 
    
        NSError *error;
    
        results = [managedObjectContext executeFetchRequest:request error:&error];
    //  NSLog(@"results %@", results);
    
        if([results count] == 0){
            NSLog(@"No results found");
            searchObj = nil;
            self.tempString = @"No results found.";
        }else{
            if ([[[results objectAtIndex:0] name] caseInsensitiveCompare:passdTextToSearchFor] == 0) {
                NSLog(@"results %@", [[results objectAtIndex:0] name]);
                searchObj = [results objectAtIndex:0];
            }else{
                NSLog(@"No results found");
                self.tempString = @"No results found.";
                searchObj = nil;
            }       
        }
    
        [tableView reloadData]; 
    
        [request release];
        [sortDescriptors release];
    
        return results;
    }
    
    - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
        textToSearchFor =  mySearchBar.text;
        results = [self SearchDatabaseForText:textToSearchFor];
        self.tempString = [myGlobalSearchObject valueForKey:@"name"];   
        NSLog(@"results count: %d", [results count]);
        NSLog(@"results 0: %@", [[results objectAtIndex:0] name]);
        NSLog(@"results 1: %@", [[results objectAtIndex:1] name]);
    }
    @end
    

    控制台打印:

    2010-06-10 16:11:18.581 XYZApp[10140:207] results count: 2
    2010-06-10 16:11:18.581 XYZApp[10140:207] results 0: BB Bugs
    2010-06-10 16:11:18.582 XYZApp[10140:207] results 1: BB Annie
    Program received signal:  “EXC_BAD_ACCESS”.
    (gdb) 
    

    编辑2:

    英国电信:

    #0  0x95a91edb in objc_msgSend ()
    #1  0x03b1fe20 in ?? ()
    #2  0x0043cd2a in -[UITableViewRowData(UITableViewRowDataPrivate) _updateNumSections] ()
    #3  0x0043ca9e in -[UITableViewRowData invalidateAllSections] ()
    #4  0x002fc82f in -[UITableView(_UITableViewPrivate) _updateRowData] ()
    #5  0x002f7313 in -[UITableView noteNumberOfRowsChanged] ()
    #6  0x00301500 in -[UITableView reloadData] ()
    #7  0x00008623 in -[SearchViewController SearchDatabaseForText:] (self=0x3d16190, _cmd=0xf02b, passdTextToSearchFor=0x3b29630) 
    #8  0x000086ad in -[SearchViewController searchBarSearchButtonClicked:] (self=0x3d16190, _cmd=0x16492cc, searchBar=0x3d2dc50)
    #9  0x0047ac13 in -[UISearchBar(UISearchBarStatic) _searchFieldReturnPressed] ()
    #10 0x0031094e in -[UIControl(Deprecated) sendAction:toTarget:forEvent:] ()
    #11 0x00312f76 in -[UIControl(Internal) _sendActionsForEventMask:withEvent:] ()
    #12 0x0032613b in -[UIFieldEditor webView:shouldInsertText:replacingDOMRange:givenAction:] ()
    #13 0x01d5a72d in __invoking___ ()
    #14 0x01d5a618 in -[NSInvocation invoke] ()
    #15 0x0273fc0a in SendDelegateMessage ()
    #16 0x033168bf in -[_WebSafeForwarder forwardInvocation:] ()
    #17 0x01d7e6f4 in ___forwarding___ ()
    #18 0x01d5a6c2 in __forwarding_prep_0___ ()
    #19 0x03320fd4 in WebEditorClient::shouldInsertText ()
    #20 0x0279dfed in WebCore::Editor::shouldInsertText ()
    #21 0x027b67a5 in WebCore::Editor::insertParagraphSeparator ()
    #22 0x0279d662 in WebCore::EventHandler::defaultTextInputEventHandler ()
    #23 0x0276cee6 in WebCore::EventTargetNode::defaultEventHandler ()
    #24 0x0276cb70 in WebCore::EventTargetNode::dispatchGenericEvent ()
    #25 0x0276c611 in WebCore::EventTargetNode::dispatchEvent ()
    #26 0x0279d327 in WebCore::EventHandler::handleTextInputEvent ()
    #27 0x0279d229 in WebCore::Editor::insertText ()
    #28 0x03320f4d in -[WebHTMLView(WebNSTextInputSupport) insertText:] ()
    #29 0x0279d0b4 in -[WAKResponder tryToPerform:with:] ()
    #30 0x03320a33 in -[WebView(WebViewEditingActions) _performResponderOperation:with:] ()
    #31 0x03320990 in -[WebView(WebViewEditingActions) insertText:] ()
    #32 0x00408231 in -[UIWebDocumentView insertText:] ()
    #33 0x003ccd31 in -[UIKeyboardImpl acceptWord:firstDelete:addString:] ()
    #34 0x003d2c8c in -[UIKeyboardImpl addInputString:fromVariantKey:] ()
    #35 0x004d1a00 in -[UIKeyboardLayoutStar sendStringAction:forKey:] ()
    #36 0x004d0285 in -[UIKeyboardLayoutStar handleHardwareKeyDownFromSimulator:] ()
    #37 0x002b5bcb in -[UIApplication handleEvent:withNewEvent:] ()
    #38 0x002b067f in -[UIApplication sendEvent:] ()
    #39 0x002b7061 in _UIApplicationHandleEvent ()
    #40 0x02542d59 in PurpleEventCallback ()
    #41 0x01d55b80 in CFRunLoopRunSpecific ()
    #42 0x01d54c48 in CFRunLoopRunInMode ()
    #43 0x02541615 in GSEventRunModal ()
    #44 0x025416da in GSEventRun ()
    #45 0x002b7faf in UIApplicationMain ()
    #46 0x00002578 in main (argc=1, argv=0xbfffef5c) at /Users/default/Documents/iPhone Projects/XYZApp/main.m:14
    

    编辑:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return[results count];
     }
    
    5 回复  |  直到 12 年前
        1
  •  3
  •   kennytm    14 年前

    没有办法 -objectAtIndex:valueForKey: . 你是说

    cell.textLabel.text = [[myArray objectAtIndex:indexPath.row] valueForKey:@"object"];
    //                     ^                               ****^
    

    ?

    要组合Objective-C方法调用,需要使用多个方括号,如 ^ 还有, -objectAtIndex: 只接受一个nsinteger,但是 indexPath 是nsindxpath。要从索引路径获取行,请使用 .row 属性,如 * s。


    编辑:

    你好像很困惑 -numberOfSectionsInTableView: 具有 -tableView:numberOfRowsInSection: . 表包含一个或多个节,每个节包含多行(表视图单元格)。

    你的桌子只有一部分,所以你应该 return 1 或者只删除此方法,因为1是默认值。实施 -TableView:行杀虫剂数量: 作为

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
       return [results count];
    }
    
        2
  •  1
  •   Philippe Leybaert    14 年前

    我认为你的语法错误。不应该是:

    cell.textLabel.text = [[myArray objectAtIndex:indexPath] valueForKey:@"object"];
    
        3
  •  0
  •   chuckSaldana    14 年前

    我在自己的代码中发现了相同的调试器消息,问题如下:

    - (void)viewDidLoad {
        [super viewDidLoad];
        products = [NSArray arrayWithObjects:@"", @"", nil];
    }
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
        return [products count];
    }
    

    这很奇怪,但是,当我使用方法计数时,应用程序崩溃了,所有问题都解决了:

    @interface theCleass:NSObject {
           NSInteger    theSections;
    }
    @end
    
    ...
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        products = [NSArray arrayWithObjects:@"", @"", nil];
    
        theSections = [products count];
    }
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
            return theSections;
     }
    

    只需获取viewdidload中元素的数量,虽然我还不知道为什么会发生这种情况,因为我可以在numberofsections中使用count方法:在早期的项目中。

        4
  •  0
  •   sth ypicasso    14 年前

    那是因为

    products = [NSArray arrayWithObjects:@"", @"", nil];
    

    是自动释放的对象 retainCount == 1 , 所以当你打电话的时候 return [products count] , the count 属性是已释放对象的属性,因此无法执行此操作。

    建议不要执行自动释放对象

    products = [[NSArray alloc] initWithObjects:@"", @"", nil];
    

    而且

    -(void)dealloc
    {
    [products release];
    [super dealloc];
    }
    

    所以当控制器弹出时,产品对象将被销毁

        5
  •  0
  •   Jahm    12 年前

    我想应该是 cell.textLabel.text = [[myArray objectAtIndex: indexPath.row] objectForKey: @"object"];