在发布之前我几乎尝试了所有内容(google,Apple Dev Doc等)。我希望在发布之前我没有错过任何内容..
我已经构建了一个有效的JSON< - > Web服务< - > iPhone登录。
使用以下代码:
NSString *responseString = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]autorelease];
NSDictionary *jsonDict = [responseString JSONValue];
NSString *loginResult = [jsonDict valueForKey:@"LoginResult"];
loginResult是员工的姓名:例如“Henning Wenger”
相应的JSON如下所示:
{"LoginResult":"S:00000412"}
现在我有一个更复杂的任务,我无法想办法解决这个问题:
我从网络服务获得以下JSON:
{"GetTeamPresentStatusResult"[
{"Firstname":"Steffen","Id":"00000456","IsPresent":"true","Lastname":"Polker"},
{"Firstname":"Erich","Id":"00000455","IsPresent":"true","Lastname":"Welter"},
{"Firstname":"Titus","Id":"00000454","IsPresent":"true","Lastname":"Sommerbeck"}, {"Firstname":"Ruediger","Id":"00000453","IsPresent":"true","Lastname":"Oelmann"},{"Firstname":"Heinz","Id":"00000452","IsPresent":"true","Lastname":"Radelfs"},{"Firstname":"Franz","Id":"00000451","IsPresent":"true","Lastname":"Wippermann"},{"Firstname":"Klaus-Dieter","Id":"00000450","IsPresent":"true","Lastname":"Just"},{"Firstname":"Alan","Id":"00000412","IsPresent":"true","Lastname":"Turing"},{"Firstname":"Konrad","Id":"00000138","IsPresent":"true","Lastname":"Zuse"},{"Firstname":"Marius","Id":"00000112","IsPresent":"true","Lastname":"Sandmann"}]}
我的处理代码开头于:
NSDictionary *jsonDict = [responseString JSONValue];
NSArray *teamStatus = [jsonDict objectForKey:@"GetTeamPresentStatusResult"];
[self BuildDataSource: nil :teamStatus];
teamStatus现在包含10个键/值对。
以下是我尝试为我的表构建数据源的方法:
- (void)BuildDataSource: (id) sender: (NSArray*)teamStatusData{
teamStatusMutableArray = [[NSMutableArray alloc]init];
teamStatusDataSection = [[NSArray alloc]initWithArray:teamStatusData];
teamStatusDictSectionOne = [NSDictionary dictionaryWithObject:teamStatusDataSection forKey:@"OrgUnits"];
[teamStatusMutableArray addObject:teamStatusDictSectionOne];
[tblView reloadData];
}
我无法弄清楚从这里走哪条路来获取我需要的数据。
我是否需要不同的数组,其中一个包含firstname / value,另一个包含lastname / value?如果是,我该怎么做?
到目前为止,我尝试了一些没有帮助的行:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
//First get the dictionary object
NSDictionary *dictionary = [teamStatusMutableArray objectAtIndex:indexPath.section];
NSString *testString = [dictionary valueForKey:@"Firstname"];
NSArray *testarray = [dictionary objectForKey:@"OrgUnits"];
NSString *test = [testarray JSONRepresentation];
test = [test stringByReplacingOccurrencesOfString:@"[" withString:@""];
test = [test stringByReplacingOccurrencesOfString:@"]" withString:@""];
NSDictionary *jsonDict = [test JSONValue];
}
我需要的是拥有一个带有可以绑定到表的员工姓名的数据源。从那里我想我可以在没有额外帮助的情况下走自己的路。
我来自.NET(C#)世界,也许这让我感到困惑。
非常感谢你..
的Henning
如果您需要更多详细信息,请询问!
SPtail的详细信息:
这是.h文件:
#import <UIKit/UIKit.h>
#import "RootViewController.h"
#import "EmployeeDetails.h"
@interface OrgUnitAA:NSObject {
NSString *FirstName;
NSString *Id;
NSString *IsPresent;
NSString *LastName;
}
@property (nonatomic, retain) NSString *FirstName;
@property (nonatomic, retain) NSString *Id;
@property (nonatomic, retain) NSString *IsPresent;
@property (nonatomic, retain) NSString *LastName;
@end
@interface TeamStatusView : UIViewController {
UITableView *tblView;
NSMutableArray *teamStatusMutableArray;
NSArray *teamStatusDataSection;
NSDictionary *teamStatusDictSectionOne;
UIViewController *EmployeeDetails;
UIViewController *RootViewController;
NSMutableData *responseData;
}
@property (nonatomic, retain) IBOutlet UITableView *tblView;
@property (nonatomic, retain) NSArray *teamStatusMutableArray;
@property (nonatomic, retain) NSMutableArray *dummyMutableArrayTimeQuota;
@property (nonatomic, retain) NSArray *teamStatusDataSection;
@property (nonatomic, retain) NSDictionary *teamStatusDictSectionOne;
@property (nonatomic, retain) IBOutlet UIViewController *RootViewController;
@property (nonatomic, retain) IBOutlet UIViewController *EmployeeDetails;
- (void)GetTeamStatus: id;
- (void)BuildDataSource: id: (NSArray*)teamStatusData;
@end
答案 0 :(得分:1)
嘿,Web服务返回的数组包含字典,这很明显。
您可以创建一个扩展NSObject并转换每个字典的类,而不是为每个属性使用不同的数组,这样您就可以将对象放在数组中而不是字典中。在这种情况下:
@interface OrgUnit:NSObject {
NSString *FirstName;
NSNumber *Id;
bool IsPresent;
NSString *LastName;
}
//And all the properties here
在此之后,您可以使用
将字典转换为对象OrgUnit *unit = [[OrgUnit alloc] init];
[unit setValuesForKeysWithDictionary:dictionary];
PS:对象中的变量名称应与字典
中的键名相同答案 1 :(得分:1)
我找到了解决方案。
这是我获取数据的方式:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
NSDictionary *dictionary = [teamStatusMutableArray objectAtIndex:indexPath.section];
NSArray *teamStatus = [dictionary objectForKey:@"OrgUnits"];
NSEnumerator *e = [teamStatus objectEnumerator];
id object;
int i = 0;
while (object = [e nextObject]) {
if(i == indexPath.row)
{
NSString *firstname = [object valueForKey:@"Firstname"];
NSString *lastname = [object valueForKey:@"Lastname"];
NSString *fullname = [NSString stringWithFormat:@"%@%@%@", lastname, @", ", firstname];
cell.textLabel.text = fullname;
bool isPresent = [object valueForKey:@"IsPresent"];
if(isPresent == true)
{
cell.imageView.image = [UIImage imageNamed:@"GreenBall.png"];
}
else
{
cell.imageView.image = [UIImage imageNamed:@"RedBall.png"];
}
break;
}
i++;
}
[aiActivity stopAnimating];
self.aiActivity.hidden = true;
return cell;
}
答案 2 :(得分:0)
可能只是一个错字。
改变这个:
teamStatusDictSectionOne = [NSDictionary dictionaryWithObject:teamStatusDataSection forKey:@"OrgUnits"];
对此:
teamStatusDictSectionOne = [NSDictionary dictionaryWithObjects:teamStatusDataSection forKey:@"OrgUnits"];
更改是-dictionaryWithObjects。复数。 Obj-C对这些东西有点挑剔。
此外,在构建单个单元格时,您将引用indexPath.section。我认为你应该引用单元格的行。该行应与您的数组索引相关联。
NSInteger row = [indexPath row];
NSDictionary *dictionary = [teamStatusMutableArray objectAtIndex:row];