पर क्लिक किए जाने पर पार्स किए गए JSON से बनाए गए सभी तालिका दृश्य दृश्यों को पुनः लोड करना मेरे पास एक काफी महत्वपूर्ण वैचारिक मुद्दा है जिसे कई लोगों ने पूछा है, लेकिन वहां आसानी से उपलब्ध स्पष्ट उत्तर नहीं है खोज कर।आईओएस रीफ्रेश बटन देखें कंट्रोलर एनवी:
मेरा आवेदन सरल है: TableViewCells की कई पंक्तियां एक पार्स किए गए JSON फ़ीड से डेटा के साथ आबादी वाली हैं। जब एक सेल पर क्लिक किया जाता है, तो उस सेल की जानकारी को SecondViewController पर भेज दिया जाता है और प्रदर्शित किया जाता है। JSON फ़ीड को एक .plist में भी संग्रहीत किया जाता है और यदि इंटरनेट उपलब्ध नहीं है, तो TableViewCells को .plist से पॉप्युलेट किया जाता है।
यह सब बढ़िया काम कर रहा है।
हालांकि, जेएसओएन फ़ीड को रीफ्रेश करने के लिए, और नए चर से नए डेटा के साथ तालिका में सभी कक्षों को रीफ्रेश करने के लिए मुझे अपने अंतिम दृश्य नियंत्रक के शीर्ष पर एक ताज़ा बटन है। हालांकि, मुझे इसे कार्यान्वित करने के साथ एक समस्या आई है:
मेरा मूल JSON कॉल, और कक्षों को पॉप्युलेट करने के लिए वेरिएबल ViewDidLoad विधि में स्थित हैं। जब दृश्य लोड होता है, तो ये चर "सेट" होते हैं और रीफ्रेश नहीं होते हैं। इसके अलावा, मैं JSON कॉल और वैरिएबल को viewWillLoad में स्थानांतरित कर सकता हूं - जो कक्ष पर क्लिक करने के बाद प्रत्येक बार तालिका को रीफ्रेश करेगा, और फिर पहले व्यू कंट्रोलर पर "बैक" पर क्लिक करके - यह JSON और कक्षों को सफलतापूर्वक अपडेट करेगा, हालांकि यह प्रभाव डालता है गति और मेनव्यू कंट्रोलर पर वापस जाने पर व्यू कंट्रोलर "रोकें" बनाता है, जो मेरे मूल JSON को कॉल करता है और मेरे चर सेट को दृश्य में एक अविभाज्य विकल्प देता है।
मैं जो एक IBAction विधि से जुड़ा हुआ है viewDidLoad में एक पुनः लोड बटन बनाया है, "ताज़ा":
बटन Programitically बनाएं viewDidLoad में:
// Reload issues button
UIBarButtonItem *button = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:@selector(refresh:)];
self.navigationItem.rightBarButtonItem = button;
[button release];
कार्रवाई विधि से जुड़ा हुआ है:
- (IBAction)refresh:(id)sender {
myRawJson = [[NSString alloc] initWithContentsOfURL:[NSURL
URLWithString:@"http://www.yoursite.com/json.JSON"]
encoding:NSUTF8StringEncoding
error:nil];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary * myParsedJson = [parser objectWithString:myRawJson error:NULL];
// New updated dictionary built from refreshed JSON
allLetterContents = [myParsedJson objectForKey:@"nodes"];
// Log the new refreshed JSON
NSLog(@"You clicked refresh. Your new JSON is %@", myRawJson);
//Maybe use the notification center?? But don't know how to implement.
//[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(refreshView:)
name:@"refreshView" object:nil];
//[[NSNotificationCenter defaultCenter] postNotificationName:@"refreshView"
object:nil];
}
[self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForVisibleRows]
withRowAnimation:UITableViewRowAnimationNone];
[myRawJson release];
}
उपरोक्त कोड में आप देख सकते हैं जब भी बटन क्लिक किया जाता है और नए JSON के साथ कंसोल करने के लिए एक संदेश लॉगिंग करता है तो मैं JSON को फिर से कॉल कर रहा हूं। यह काम कर रहा है मैंने एक शब्दकोश भी फिर से बनाया है जो सफलतापूर्वक नई सामग्री जोड़ रहा है।
मेरा सवाल है: मैं इस नए डेटा के साथ तालिकाव्यूकल्स "रीफ्रेश" कैसे कर सकता हूं? क्या मैं बटन को पूरे व्यू कंट्रोलर को फिर से लोड कर सकता हूं - तो यह फिर से ViewDidLoad को कॉल करेगा? क्या मुझे अपने ऐप्स की संरचना को फिर से सोचने की ज़रूरत है, या मेरे मूल चर को देखने के बाहर देखने की ज़रूरत है?
मैं NSNotificationCenter पर कुछ पोस्ट पढ़ रहा हूं, लेकिन इसका कार्यान्वयन अभी भी मुझे परेशान करता है, क्योंकि मैं आईओएस विकास के लिए बिल्कुल नया हूं।
धन्यवाद ~
अद्यतन:
यह अभी भी अपडेट नहीं हो रहा है। [Self.tableView reloadData] के साथ मेरा पूरा ताज़ा बटन कोड यहां दिया गया है; मेरे आईबीएक्शन के अंत में बुलाया।
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
}
// Configure the cell.
cell.textLabel.text = [self.contentTitleArray objectAtIndex: [indexPath row]];
cell.detailTextLabel.text = [self.contentNidArray objectAtIndex: [indexPath row]];
return cell;
}
didSelectRowAtIndexPath पर यह निर्धारित करना::
- (IBAction)refresh:(id)sender {
[DSBezelActivityView newActivityViewForView:
self.navigationController.navigationBar.superview
withLabel:@"Loading Feed..." width:160];
myRawJson = [[NSString alloc] initWithContentsOfURL:[NSURL
URLWithString:@"http://site.com/mobile.JSON"]
encoding:NSUTF8StringEncoding
error:nil];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary * myParsedJson = [parser objectWithString:myRawJson error:NULL];
allLetterContents = [myParsedJson objectForKey:@"nodes"];
BOOL isEmpty = ([myParsedJson count] == 0);
if (isEmpty) {
NSString *refreshErrorMessage = [NSString
stringWithFormat:@"An internet or network connection is required."];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Alert"
message: refreshErrorMessage
delegate:self
cancelButtonTitle:@"Close"
otherButtonTitles:nil];
[alert show];
[alert release];
allLetterContents = [NSMutableDictionary
dictionaryWithContentsOfFile:[self saveFilePath]];
//NSLog(@"allLetterContents from file: %@", allLetterContents);
} else {
NSLog(@"Your new allLetterContents is %@", allLetterContents);
// Fast enumeration through the allLetterContents NSMutableDictionary
for (NSMutableDictionary * key in allLetterContents) {
NSDictionary *node = [key objectForKey:@"node"];
NSMutableString *contentTitle = [node objectForKey:@"title"];
NSMutableString *contentNid = [node objectForKey:@"nid"];
NSMutableString *contentBody = [node objectForKey:@"body"];
// Add each Title and Nid to specific arrays
//[self.contentTitleArray addObject:contentTitle];
[self.contentTitleArray addObject:[[contentTitle
stringByReplacingOccurrencesOfString:@"&"
withString:@"&"] mutableCopy]];
[self.contentNidArray addObject:contentNid];
[self.contentBodyArray addObject:contentBody];
}
}
[self.tableView reloadData];
[DSBezelActivityView removeViewAnimated:YES];
[myRawJson release];
}
मैं cellForRowAtIndexPath पर सेल (: प्रकाशित किया गया था पूरे विधिअद्यतन किया गया) को विन्यस्त कर रहा हूँ
self.detailViewController.currentNodeTitle = [contentTitleArray
objectAtIndex:indexPath.row];
self.detailViewController.currentNodeNid= [contentNidArray
objectAtIndex:indexPath.row];
self.detailViewController.currentNodeBody = [contentBodyArray
objectAtIndex:indexPath.row];
तो जब मेरी ताज़ा करें बटन पर क्लिक तालिका को नए जेसन के साथ ताज़ा करना चाहिए, लेकिन नहीं .. क्या मुझे एक कदम याद आ रहा है?
साथ ही इस महत्वपूर्ण नहीं हो सकता है, लेकिन मैं के साथ हर दूसरे पंक्ति में रंगों को बदलकर हूँ:
// Customize the appearance of table view cells.
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row % 2)
{
[cell setBackgroundColor:[UIColor colorWithRed:221.0/255.0 green:238.0/255.0 blue:255.0/255.0 alpha:1]];
cell.textLabel.textColor = [UIColor colorWithRed:2.0/255.0 green:41.0/255.0 blue:117.0/255.0 alpha:1];
cell.detailTextLabel.textColor = [UIColor colorWithRed:2.0/255.0 green:41.0/255.0 blue:117.0/255.0 alpha:1];
} else [cell setBackgroundColor:[UIColor clearColor]];
}
अद्यतन
मुझे 'cellForRowAtIndexPath' के साथ कुछ समस्या पर संदेह होगा: जिसे आपने केवल एक छोटा टुकड़ा चिपकाया है। – Krizz
@ Krizz - मैंने अपना पूरा सेलफॉररोएट इंडेक्सपैथ जोड़ा। मैंने पूरी विधि पोस्ट नहीं की थी क्योंकि मैंने जो जोड़ा था उसे छोड़कर यह पूरी तरह से मानक था। - धन्यवाद – Pat
क्या आपने डीबगर के साथ सत्यापित किया है कि '[self.tableView reloadData]; 'पहुंच रहा है और' tableView' शून्य नहीं है? – Krizz