How to create a NSDictionary in NSMutableArray in iPhone?
I have one NSMutableDictionary and it contains the user details. I have used parsing and get the data from the MutableDictionary.
feedDictionary is, {
"city = New York",
"phone = 111111",
"email = aaa@test.com",
"year = 1986",
"degree = Undergraduate"
}
I have added all the value into the FeedArray and i want to create the two dictionary for the FeedArray.Because i have displayed the data in the section table view. so the table section data is - Section-1 -[city,phone,email] and Section-2-[year,degree]. Because if the anyone of the data is nil, so i shouldn't remove that particular rows in that section. so that i want to check the data, whether the data is nil or not.
Edit:
Before i added to the FeedArray, i want to check the string is nil or not. If the string is nil, then i shouldn't add the Array and if the data is not nil, then only added to the 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"]]?:开发者_JAVA百科[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;
}
Expected output is,
My array is 5 (
section1{
"city = New York",
"phone = 111111",
"email = aaa@test.com",
}
section2
{
"year = 1986",
"degree = Undergraduate"e"
}
) So how can i create the dictionary for the Mutable Array. So Please guide me.
Thanks!
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];
your UITableView Datasource methods:
- (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;
}
Edit: I guess i misunderstood your edited question. your code looks fine at first sight.
精彩评论