2010-06-10 90 views
0

更新:這似乎是「結果」範圍的問題。 titleForHeaderInSection似乎不知道「結果」數組內容。我會找到一個解決方案,以防萬一任何人在同一個問題中遇到此問題時會發布。Objective-C:範圍問題cellForRowAtIndexPath

如何將cellForRowAtIndexPath中的每個單獨行設置爲由Fetch請求填充的數組結果? (取指按下按鈕時作出請求。)

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

BT:

#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]; 
} 

回答

3

有沒有方法-objectAtIndex:valueForKey:。你的意思是

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

要組合Objective-C方法調用,您需要多個括號,如^'s所示。此外,-objectAtIndex:只接受一個NSInteger,但indexPath是一個NSIndexPath。要從索引路徑獲取該行,請使用.row屬性,如*所示。


編輯:

你似乎混淆-numberOfSectionsInTableView:-tableView:numberOfRowsInSection:。一個表格包含一個或多個部分,每個部分包含許多行(表格視圖單元格)。

您的表格只有1個部分,所以您應該return 1,或者僅刪除此方法,因爲1是默認值。並執行-tableView:numberOfRowsInSection:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [results count]; 
} 
+0

如果我把它,我的應用程序崩潰的「EXC_BAD_ACC ESS」。這可能與其他事情有關。我會收集更多信息後發佈。感謝您的幫助。 – 2010-06-10 19:43:09

+0

@Mr:是的,你需要展示你如何填充'myArray'。 – kennytm 2010-06-10 19:45:46

+0

免責聲明:同事在我的辦公桌旁改變了名字。必須等待整整一個月才能將其更改爲正常。上面張貼的代碼請求。 – 2010-06-10 19:51:44

1

我認爲你的語法錯了。它不應該是:

cell.textLabel.text = [[myArray objectAtIndex:indexPath] valueForKey:@"object"]; 
+1

** indexPath.row ** - 常見錯誤。 'objectAtIndex'對於新開發者來說真的很混亂,因爲函數實際上會說「給我一個indexPath,並且我會給你一個對象」。 – Emil 2010-06-10 22:40:21

0

我發現了同樣的消息調試器在我自己的代碼和問題是以下幾點:

- (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:在早期的項目中。

+1

我的猜測是你沒有保留產品。 'arrayWithObjects'返回一個autorelease對象...所以當你進入'numberOfSectionsInTableView'時,產品可能已經被釋放。 – livingtech 2010-10-01 15:05:18

0

這是因爲

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

會被自動釋放的對象與retainCount == 1, 所以當你打電話return [products count],該count屬性是一個已釋放對象的屬性,所以你不能做到這一點。

的建議是不要做自動釋放對象

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

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

這樣的產品對象將被毀壞了,當控制器會彈出UPED

0

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