簡體   English   中英

如何在iPhone中的NSMutableArray中創建NSDictionary?

[英]How to create a NSDictionary in NSMutableArray in iPhone?

我有一個NSMutableDictionary,它包含用戶詳細信息。 我使用了解析並從MutableDictionary獲取數據。

feedDictionary是,{

"city = New York",
"phone = 111111",
"email = aaa@test.com",
"year = 1986",
"degree = Undergraduate"

}

我已將所有值添加到FeedArray中,並且我想為FeedArray創建兩個字典。因為我已在節表視圖中顯示數據。 所以表格部分數據是 - 第1節 - [城市,電話,電子郵件]和第2節[年份,學位]。 因為如果任何數據都為零,那么我不應該刪除該部分中的特定行。 這樣我想檢查數據,無論數據是否為零。

編輯:

在添加到FeedArray之前,我想檢查字符串是否為nil。 如果字符串為nil,那么我不應該添加數組,如果數據不是nil,則只添加到FeedArray。

[self isEmpty:[feedDictionary valueForKey:@"city"]]?:[feedArray addObject:[feedDictionary valueForKey:@"city"]]; //section1

[self isEmpty:[feedDictionary valueForKey:@"phone"]]?:[feedArray addObject:[feedDictionary valueForKey:@"phone"]]; //section1

[self isEmpty:[feedDictionary valueForKey:@"email"]]?:[feedArray addObject:[feedDictionary valueForKey:@"email"]]; //section1

[self isEmpty:[feedDictionary valueForKey:@"year"]]?:[feedArray addObject:[feedDictionary valueForKey:@"year"]]; //section2

[self isEmpty:[feedDictionary valueForKey:@"degree"]]?:[feedArray addObject:[feedDictionary valueForKey:@"degree"]]; //section2

 -(BOOL) isEmpty :(NSString*)str{
      if(str == nil || [[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0)
    return YES;
return NO;

}

預期的輸出是

我的數組是5(

section1{
"city = New York",
"phone = 111111",
"email = aaa@test.com",
}
 section2
{

"year = 1986",
"degree = Undergraduate"e"
}

那么如何為Mutable Array創建字典呢? 所以,請指導我。

謝謝!

NSArray *section0Keys = [NSArray arrayWithObjects:@"city", @"phone", @"email", nil];
NSMutableArray *section0 = [[[feedDictionary objectsForKeys:section0Keys notFoundMarker:[NSNull null]] mutableCopy] autorelease];
if ([section0 indexOfObject:[NSNull null]] != NSNotFound) {
    isValid = NO;
}
// you don't need this if you don't care for invalid arrays.
[section0 removeObjectsInArray:[NSArray arrayWithObject:[NSNull null]]];

NSArray *section1Keys = [NSArray arrayWithObjects:@"year", @"degree", nil];
NSMutableArray *section1 = [[[feedDictionary objectsForKeys:section1Keys notFoundMarker:[NSNull null]] mutableCopy] autorelease];
if ([section1 indexOfObject:[NSNull null]] != NSNotFound) {
    isValid = NO;
}
[section1 removeObjectsInArray:[NSArray arrayWithObject:[NSNull null]]];

self.dataArray = [NSArray arrayWithObjects:
section0,
section1,
nil];

您的UITableView數據源方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.dataArray count];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[self.dataArray objectAtIndex:section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    /.../
    cell.textLabel.text = [[self.dataArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    return cell;
}

編輯:我想我誤解了你編輯過的問題。 乍一看,您的代碼看起來不錯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM