【问题标题】:NSArray With NSString Is This Even Possible?NSArray 和 NSString 这甚至可能吗?
【发布时间】:2011-08-15 07:36:01
【问题描述】:

我正在尝试构建分组剖视图。我想将数组设置为字典中的对象,但我遇到了空数组问题。 这甚至可能吗????有什么诀窍吗????

#import "RootViewController.h"

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    // Get the specific Node for this row.
    Nodes *nodes = (Nodes *)[nodesMArray objectAtIndex:indexPath.row];
    // Create the Details View Controller and initialize it. 
    nodesDetailView *viewController = [[nodesDetailView alloc] initWithStyle:UITableViewStyleGrouped ];
    [[self navigationController] pushViewController:viewController animated:YES];   

    // Set the title of the view to the nodes name
    viewController.title = [nodes computer_name];
    //Information
    //Network Data
    viewController.ipaddress= [nodes ipaddress];
    viewController.subnet_mask= [nodes subnet_mask];
    viewController.gateway= [nodes gateway];
    viewController.domain_name= [nodes domain_name];
}

#import "nodesDetailViewController. h


@interface nodesDetailView : UITableViewController{

//Network Data
NSString *ipaddress;
NSString *subnet_mask;
NSString *gateway;
NSString *domain_name;

//Grouped 
NSMutableArray *Keys;
NSMutableDictionary *Contents;

}

@property (nonatomic, retain) NSMutableArray *Keys;
@property (nonatomic, retain) NSMutableDictionary *Contents;



@property (nonatomic, retain) NSString *ipaddress;
@property (nonatomic, retain) NSString *subnet_mask;
@property (nonatomic, retain) NSString *gateway;
@property (nonatomic, retain) NSString *domain_name;



@end

#import "nodesDetailViewController. m


@synthesize .......


- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {

    NSMutableArray *keys = [[NSMutableArray alloc] init];
    NSMutableDictionary *contents = [[NSMutableDictionary alloc] init];


    NSString *network = @"Network Data";
    NSString *product = @"Product Details";

  //IS THIS POSSIBLE ??????? Because I get An Empty array 
    [contents setObject:[NSArray arrayWithObjects: ipaddress,subnet_mask, gateway, domain_name, nil] forKey:network];

 return self;
}

提前致谢。

【问题讨论】:

  • 请把你的问题说清楚...
  • NSlog it and I get The content of arry is{ "General Information" = ( ); "网络数据" = ( ); "产品详情" = ( ); }
  • 请在您访问此数组的位置添加代码。据我所知,您的字典中的数组很好,但您访问它的方式错误。
  • - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSString *key = [[self sectionKeys] objectAtIndex:section]; NSArray *contents = [[self sectionContents] objectForKey:key];返回[内容计数]; }

标签: iphone xcode nsstring nsarray nsdictionary


【解决方案1】:

您添加到数组中的所有这些字符串甚至还没有被分配/初始化。这就是数组为空的原因。你不能只去:NSString *emptyString;,然后去:[myArray addObject:emptyString];。你的字符串都是空的。

【讨论】:

    【解决方案2】:

    检查您从表格视图传递的值。他们可能为零。我刚刚使用了这么多数据,并且得到了正确的 NSLog。

    NSMutableDictionary *contents = [[NSMutableDictionary alloc] init];
    NSString *my = @"kk";
    NSString *my1 = @"polko";
    [contents setObject:[NSArray arrayWithObjects: my1,my, nil] forKey:@"o"];
    NSLog(@"%@" , contents);
    

    【讨论】:

    • 我已经这样做了,而且效果很好。问题是它不适用于 viewController.ipaddress= [nodes ipaddress];数组中的 ipaddress 什么也没给我。但是,如果我创建一个 UILabel 并执行诸如 headerLabel.text = ipaddress; 之类的操作工作正常为什么不能在数组中工作???
    • 您将 [nodes ipaddress] 设为 nil.. 吗?你有 NSLog 这条语句吗
    【解决方案3】:

    对于那些想知道的人:问题已解决 我的代码很好。问题是: 它在错误的地方。

    代码应该在 - (void)viewDidLoad 而不是 - (id)initWithStyle:

    我要感谢所有花时间研究它的人。 我不知道为什么我把代码放在那里。

    再次感谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-14
      • 1970-01-01
      • 1970-01-01
      • 2014-11-21
      • 1970-01-01
      相关资源
      最近更新 更多