0

我的应用程序中有一个实体/类,其中填充了 JSON 调用。进行此调用后,我会在我的应用程序委托中设置一些值,这些值不会通过迭代此实体而经常更改。所有这一切都很好,只是给出了背景故事......一旦用户点击我的表格视图中的一个项目,我想将我分配给 0 代表 false 或 1 代表 true 的字符串值用作详细屏幕表格视图中所选值的索引,在各自的单元格中分别为“否”和“是”。基本上,我试图向用户展示当前设置是什么,如果他们想更改它,我将允许用户更改他们的选择并将其保存回上一页的表格视图以及对我的实体的更新。

我遇到的问题是详细屏幕的表格视图没有选择两行,而不仅仅是选定的索引。我需要先解决这个问题。

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

我意识到这是一团糟……我只需要一些方向。

这是我的主屏幕和详细信息屏幕的用户界面...

主要的 详细信息...注意两个项目都已检查...

4

2 回答 2

1

我做了以下测试项目来做我认为您正在尝试做的事情,但是使用我自己的设计我认为可以简化一些事情,并且似乎工作正常。设计如下:

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];
}
于 2012-10-14T04:42:51.473 回答
0

很难说你在做什么,但是这一行:

if ((int)indexPath.row == (int)[appDelegate.frozen_quantity_value isEqualToString:@"No"] ? @"0" : @"1")

将始终评估为“true”,因为三元运算符将返回一个字符串“0”或“1”,但字符串是有效对象,因此它会评估为 true。这可能不是你的问题,但我真的不知道你想用那段代码做什么——例如,什么是 unselectCellAtIndexPath:withObject:? 你没有显示代码。

于 2012-10-12T16:46:18.277 回答