我的应用程序中有一个实体/类,其中填充了JSON调用。一旦进行了这个调用,我在我的应用程序委托中设置了一些值,这些值不会经常通过迭代这个实体而改变。所有这些都很有效,只是给出了背景故事....一旦用户点击我的表视图中的项目,我想把我分配给0的字符串值设为false或1设为true,并将其用作详细屏幕表视图中所选值的索引,它们各自在单元格中为“否”,然后为“是”。基本上我试图向用户显示当前设置是什么,如果他们想要更改它,我将允许用户更改他们的选择并将其保存回上一页的表格视图以及对我的实体的更新。
我遇到的问题是细节屏幕的表视图没有选择BOTH行,而不仅仅是选择的索引。我需要先解决这个问题。
cellForRowAtIndexPath - 似乎工作正常......
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *result = nil;
static NSString *CellIdentifier = @"CellIdentifier";
result = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (result == nil){
result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
Settings *settings = (Settings *)[appDelegate.settings objectAtIndex:indexPath.row];
// Save the value the user clicked on for use in updating the MasterViewController and the data model
result.textLabel.text = settings.name;
NSLog(@"result.textLabel.text: %@", result.textLabel.text);
return result;
}
这就是事情变得丑陋的地方!
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"appDelegate.selectedValueInSettingsCheckedIndexValue: %i", (int)appDelegate.selectedValueInSettingsCheckedIndexValue);
NSLog(@"appDelegate.frozen_quantity_value: %@", appDelegate.frozen_quantity_value == @"No" ? @"0" : @"1");
if(appDelegate.selected_category == FROZEN && appDelegate.selected_setting == QUANTITY && [appDelegate.frozen_quantity_value isEqualToString:ZERO]) {
NSLog(@"Reached frozen quantity with value of: %@", appDelegate.frozen_quantity_value == @"No" ? @"0" : @"1");
NSLog(@"indexPath.row: %i", indexPath.row);
if ((int)appDelegate.frozen_quantity_value == (int)appDelegate.selectedValueInSettingsCheckedIndexValue)/*(int)[appDelegate.frozen_quantity_value isEqualToString:@"No"] ? @"0" : @"1")*/ {
[cell setSelected:NO animated:YES];
[self performSelector:@selector(unselectCellAtIndexPath:) withObject:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
[cell setSelected:NO];
cell.accessoryType = UITableViewCellAccessoryNone;
}
} else if (appDelegate.selected_category == FROZEN && appDelegate.selected_setting == FACINGS) {
NSLog(@"Reached vegetables facings with value of: %@", appDelegate.vegetables_facing_value == @"No" ? @"0" : @"1");
NSLog(@"indexPath.row: %i", indexPath.row);
}
}
更新的代码......
-(void)unselectCellAtIndexPath:(NSIndexPath *)indexPath{
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
我意识到这是个混乱......我只是需要一些方向。
以下是我的UI在Main和Detail屏幕中的样子......
答案 0 :(得分:1)
我做了以下测试项目来做我认为你想要做的事情,但是使用我自己的设计,我认为这样可以简化一些事情,并且似乎工作得很好。设计如下:
1)Apple不建议从像appown这样的单例获取表视图的数据,所以我在该类中唯一做的就是为窗口设置根视图控制器:
- (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,创建Setting对象和数组来保存它们。然后它会发布包含数据数组的通知。现在,该类只是模拟数据,因为我无法访问您的服务器。我调用该方法从表视图控制器开始下载(模拟)。
-(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];
}
答案 1 :(得分:0)
很难说出你在做什么,但这一行:
if ((int)indexPath.row == (int)[appDelegate.frozen_quantity_value isEqualToString:@"No"] ? @"0" : @"1")
将始终求值为“true”,因为三元运算符将返回字符串“0”或“1”,但字符串是有效对象,因此它将计算为true。这可能不是你的问题,但我真的不知道你要用这段代码做什么 - 例如,什么是unselectCellAtIndexPath:withObject:?你没有显示相应的代码。