NSMutableDictionary 和 NSArray

发布于 2024-11-14 18:20:53 字数 2244 浏览 3 评论 0原文

我正在分割一个 String:

@"Sam|26|Developer,Hannah|22|Team Leader,Max|1|Dog"

并使用 NSMutableDictionary 在带有 3 个标签的 TableViewCell 中显示。代码:

- (void)viewDidLoad {
    [super viewDidLoad];

NSString *test = @"Sam Parrish|26|Developer,Hannah Rajamets|22|Team Leader,Max Parrish|1|Dog";

testArray = [[NSArray alloc] init];
testArray = [test componentsSeparatedByString:@","];

dict = [NSMutableDictionary dictionary];
    for (NSString *s in testArray) {

        testArrayNew = [s componentsSeparatedByString:@"|"];
        [dict setObject:[testArrayNew objectAtIndex:1] forKey:[testArrayNew objectAtIndex:0]];
        [dict setObject:[testArrayNew objectAtIndex:2] forKey:[testArrayNew objectAtIndex:1]];

        NSLog(@"Dictionary: %@", [dict description]);
    }

[dict retain];

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[dict allKeys] count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"CustomCell";

    untitled *cell = (untitled *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"untitled" owner:nil options:nil];

        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                cell = (untitled *) currentObject;
                break;
            }
        }
    }

    // Configure the cell.
    cell.nameLabel.text = [[dict allKeys] objectAtIndex:[indexPath row]];
    cell.ageLabel.text = [dict objectForKey:cell.nameLabel.text];
    cell.jobLabel.text = [dict objectForKey:cell.ageLabel.text];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

我运行时显示 6 个 TableViewCell,其中 3 个是完美的,另外 3 个是数据的分类。我意识到这与 setObject: forKey: 有关,但似乎无法找到使其正常工作的解决方案。

非常感谢任何帮助..

山姆

I am splitting a String:

@"Sam|26|Developer,Hannah|22|Team Leader,Max|1|Dog"

and using NSMutableDictionary to display in a TableViewCell with 3 labels. Code:

- (void)viewDidLoad {
    [super viewDidLoad];

NSString *test = @"Sam Parrish|26|Developer,Hannah Rajamets|22|Team Leader,Max Parrish|1|Dog";

testArray = [[NSArray alloc] init];
testArray = [test componentsSeparatedByString:@","];

dict = [NSMutableDictionary dictionary];
    for (NSString *s in testArray) {

        testArrayNew = [s componentsSeparatedByString:@"|"];
        [dict setObject:[testArrayNew objectAtIndex:1] forKey:[testArrayNew objectAtIndex:0]];
        [dict setObject:[testArrayNew objectAtIndex:2] forKey:[testArrayNew objectAtIndex:1]];

        NSLog(@"Dictionary: %@", [dict description]);
    }

[dict retain];

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[dict allKeys] count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"CustomCell";

    untitled *cell = (untitled *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"untitled" owner:nil options:nil];

        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                cell = (untitled *) currentObject;
                break;
            }
        }
    }

    // Configure the cell.
    cell.nameLabel.text = [[dict allKeys] objectAtIndex:[indexPath row]];
    cell.ageLabel.text = [dict objectForKey:cell.nameLabel.text];
    cell.jobLabel.text = [dict objectForKey:cell.ageLabel.text];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

this is displaying 6 TableViewCells when I run, 3 of which are perfect, the other 3 are an assortment of the data. i realise this is to do with the setObject: forKey: but can't seem to find the solution for this to work correctly.

any help much appreciated..

sam

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

记忆で 2024-11-21 18:20:53

我会将数据存储为持有 NSArrays 的 NSArray...

- (void)viewDidLoad {
        [super viewDidLoad];

        NSString *test = @"Sam Parrish|26|Developer,Hannah Rajamets|22|Team Leader,Max Parrish|1|Dog";

        data = [[NSMutableArray alloc] init];

        for (NSString *s in testArray) {
            [data addObject:[s componentsSeparatedByString:@"|"]];
        }
}

然后在

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

使用

// Configure the cell.
NSArray *cellData = [data objectAtIndex:indexPath.row];
cell.nameLabel.text = [cellData objectAtIndex:0];
cell.ageLabel.text = [cellData objectAtIndex:1];
cell.jobLabel.text = [cellData objectAtIndex:2];

I would store the data as an NSArray holding NSArrays....

- (void)viewDidLoad {
        [super viewDidLoad];

        NSString *test = @"Sam Parrish|26|Developer,Hannah Rajamets|22|Team Leader,Max Parrish|1|Dog";

        data = [[NSMutableArray alloc] init];

        for (NSString *s in testArray) {
            [data addObject:[s componentsSeparatedByString:@"|"]];
        }
}

Then in

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

use

// Configure the cell.
NSArray *cellData = [data objectAtIndex:indexPath.row];
cell.nameLabel.text = [cellData objectAtIndex:0];
cell.ageLabel.text = [cellData objectAtIndex:1];
cell.jobLabel.text = [cellData objectAtIndex:2];
沫离伤花 2024-11-21 18:20:53

如果我理解正确的话,你是说前三个单元格是正确的,而其他三个单元格根本不应该显示?如果是这样的话,问题就在于:

return [[dict allKeys] count];

难道不应该……

return [testArray count];

这种情况下,你需要保留testArray

此外,您还存在内存泄漏。您创建 NSArray 的实例,然后通过将 componentsSeparatedByString: 的返回值分配给同一个变量来立即泄漏它。您是否对代码运行了静态分析器?

If I understand you correctly, you're saying that the first three cells are correct and the other three shouldn't be shown at all? If that's the case, the problem is this:

return [[dict allKeys] count];

Shouldn't it be...

return [testArray count];

...in which case, you need to retain testArray.

In addition, you have a memory leak. You create an instance of NSArray, then you immediately leak it by assigning the return value of componentsSeparatedByString: to the same variable. Have you run the static analyser over your code?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文