我做了以下测试项目来做我认为您正在尝试做的事情,但是使用我自己的设计我认为可以简化一些事情,并且似乎工作正常。设计如下:
1) Apple 不建议从像应用程序委托这样的单例中获取表视图的数据,因此我在该类中所做的唯一一件事就是为窗口设置根视图控制器:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
TableController *tc = [[TableController alloc]initWithNibName:@"TableController" bundle:nil];
tc.title = @"Main Table";
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:tc];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
2) 我创建了一个类,DownloadFromServer,它将执行下载、解析 JSON、创建设置对象和保存它们的数组。然后它发布一个包含数据数组的通知。就目前而言,该类只是模拟数据,因为我无权访问您的服务器。我调用该方法从表视图控制器开始下载(模拟)。
-(void) connectionDidFinishLoading { //:(NSURLConnection *)connection {
self.settings = [NSMutableArray array];
//NSError *error = nil;
//id result = [NSJSONSerialization JSONObjectWithData:self.receivedData options:kNilOptions error:&error];
//Simulated data here
NSMutableArray *result = [NSMutableArray array];
[result addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:@2377,@"CatalogID",@"Frozen",@"Category",@"0",@"Facings",@"25",@"ID",@"0",@"Quantity", nil]];
[result addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:@2377,@"CatalogID",@"Fruit",@"Category",@"0",@"Facings",@"19",@"ID",@"0",@"Quantity", nil]];
[result addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:@2377,@"CatalogID",@"Salads",@"Category",@"1",@"Facings",@"12",@"ID",@"1",@"Quantity", nil]];
[result addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:@2377,@"CatalogID",@"Vegetables",@"Category",@"1",@"Facings",@"26",@"ID",@"0",@"Quantity", nil]];
if ([result isKindOfClass:[NSArray class]]) {
for (NSDictionary *item in result) {
NSString *settingsID = [item objectForKey:@"ID"];
NSString *category = [item objectForKey:@"Category"];
NSString *categoryID = [item objectForKey:@"CatalogID"];
NSString *facings = [item objectForKey:@"Facings"];
NSString *quantity = [item objectForKey:@"Quantity"];
Setting *setting = [[Setting alloc] initWithName:settingsID desc:category CategoryID:categoryID Facings:facings Quantity:quantity];
[self.settings addObject:setting];
}
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"UpdateReceived" object:self userInfo:@{@"array" : self.settings}];
}
3) 表格视图控制器开始下载并从通知中发送的数据填充其本地数组。在 tableView:didSelectRowAtIndexPath: 方法中,我决定详细信息表修改选择的最佳方法是传递设置对象以及所选行的 indexPath——这为 DetailViewController 提供了修改所需的所有信息数据。
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateArray:) name:@"UpdateReceived" object:nil];
DownloadFromServer *downloader = [[DownloadFromServer alloc]init];
[downloader connectionDidFinishLoading];
}
-(void)viewDidAppear:(BOOL)animated {
[self.tableView reloadData];
}
-(void)updateArray:(NSNotification *) aNote {
self.theData = [aNote.userInfo valueForKey:@"array"];
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.theData.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [[self.theData objectAtIndex:section] category];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if (indexPath.row ==0) {
cell.textLabel.text = @"Facings";
cell.detailTextLabel.text = [[NSString stringWithFormat:@"%@",[[self.theData objectAtIndex:indexPath.section] facings]] intValue] ? @"YES" : @"NO";
}else{
cell.textLabel.text = @"Quantity";
cell.detailTextLabel.text = [[NSString stringWithFormat:@"%@",[[self.theData objectAtIndex:indexPath.section] quantity]] intValue] ? @"YES" : @"NO";
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
detailViewController.selectedIndexPath = indexPath;
detailViewController.settingObject = [self.theData objectAtIndex:indexPath.section];
[self.navigationController pushViewController:detailViewController animated:YES];
}
在 DetailViewController 中,我这样做是为了让用户可以通过在表中选择一行来编辑 NO/YES 值——根据选择了哪一行,以及在主表中选择了哪一行,逻辑会更新值并重新加载桌子。当您点击后退按钮时,主表也会通过 viewDidAppear 方法进行更新。
#import "DetailViewController.h"
#import "Setting.h"
@implementation DetailViewController
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
switch (self.selectedIndexPath.row) {
case 0:
return [NSString stringWithFormat:@"%@ - Facings", self.settingObject.category];
break;
case 1:
return [NSString stringWithFormat:@"%@ - Quantity", self.settingObject.category];
break;
default:
return @"";
break;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"DetailCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
int isYes = (self.selectedIndexPath.row ? self.settingObject.quantity.intValue : self.settingObject.facings.intValue);
if (indexPath.row ==0) {
cell.textLabel.text = @"NO";
cell.accessoryType = (isYes) ? UITableViewCellAccessoryNone : UITableViewCellAccessoryCheckmark;
}else{
cell.textLabel.text = @"YES";
cell.accessoryType = (isYes) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int test = indexPath.row + (2 * self.selectedIndexPath.row);
switch (test) {
case 0:
self.settingObject.facings = @"0";
break;
case 1:
self.settingObject.facings = @"1";
break;
case 2:
self.settingObject.quantity = @"0";
break;
case 3:
self.settingObject.quantity = @"1";
break;
default:
break;
}
[self.tableView reloadData];
}