Pat*_*Pat 6 objective-c uitableview nsnotifications uiviewcontroller ios
我有一个相当重要的概念问题,许多人已经问过,但搜索找不到一个现成的明确答案.
我的应用程序很简单:几行TableViewCells填充了来自解析的JSON提要的数据.单击一个单元格时,该单元格的信息将传递给SecondViewController并显示.JSON提要也存储在.plist中,如果Internet不可用,则从.plist填充TableViewCell.
这一切都很有效.
但是,我需要的最后一件事是在我的FirstViewController顶部的刷新按钮来刷新JSON提要,并且表中的所有单元格都包含来自新变量的新数据.但是,我遇到了实现这个的问题:
我原来的JSON调用和填充单元格的变量位于ViewDidLoad方法中.加载视图时,这些变量将"设置"并且不会刷新.此外,我可以将JSON调用和变量移动到viewWillLoad中 - 每次单击一个单元格后刷新表格,然后单击"返回"到firstViewController - 这将成功更新JSON和单元格,但它确实会影响回到MainViewController时,速度并使视图控制器"暂停",这使得调用我的原始JSON并在viewWillLoad中设置我的变量是一个不可行的选项.
我在ViewDidLoad中创建了一个重新加载按钮,它链接到IBAction方法"refresh":
在ViewDidLoad中以Programitically方式创建按钮:
// Reload issues button
UIBarButtonItem *button = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:@selector(refresh:)];
self.navigationItem.rightBarButtonItem = button;
[button release];
Run Code Online (Sandbox Code Playgroud)
与之相关的行动方法:
- (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];
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,您可以看到每次单击按钮时我都会重新调用JSON,并使用新的JSON将消息记录到控制台.这很有效.我甚至重新构建了一个成功添加新内容的字典.
我的问题是:如何使用这些新数据使tableViewCells"刷新"?我可以让按钮重新加载整个视图控制器 - 所以它会再次调用ViewDidLoad吗?我是否需要重新考虑我的应用程序结构,或者将原始变量移出viewDidLoad?
我一直在阅读NSNotificationCenter上的一些帖子,但是这个的实现仍然令我感到困惑,因为我对iOS开发还很新.
谢谢〜
更新:
它仍然没有更新.这是我的[self.tableView reloadData]的完整刷新按钮代码; 在我的IBAction结束时调用.
- (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];
}
Run Code Online (Sandbox Code Playgroud)
我正在cellForRowAtIndexPath配置单元格(更新:发布整个方法):
- (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;
}
Run Code Online (Sandbox Code Playgroud)
在didSelectRowAtIndexPath上设置它:
self.detailViewController.currentNodeTitle = [contentTitleArray
objectAtIndex:indexPath.row];
self.detailViewController.currentNodeNid= [contentNidArray
objectAtIndex:indexPath.row];
self.detailViewController.currentNodeBody = [contentBodyArray
objectAtIndex:indexPath.row];
Run Code Online (Sandbox Code Playgroud)
因此,当点击我的刷新按钮时,表格应该用新的json刷新,但不是..我错过了一步吗?

此外,这可能并不重要,但我正在改变其他每一行的颜色:
// 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]];
}
Run Code Online (Sandbox Code Playgroud)
更新

小智 5
您需要调用重载方法.
[self.tableView reloadData];
Run Code Online (Sandbox Code Playgroud)
这将触发dataSource和delegate事件将刷新UITableView.
您可以在UITableView类参考中找到更多信息:
调用此方法可重新加载用于构造表的所有数据,包括单元格,节页眉和页脚,索引数组等.为了提高效率,表视图仅重新显示可见的行.
| 归档时间: |
|
| 查看次数: |
10304 次 |
| 最近记录: |