我正在开发一个 iphone 应用程序。在此我必须根据来自服务器的字段信息动态生成用户配置文件表单。
因此,如果有 5 个字段作为响应,我想从这些数据中创建 5 个标签以显示在uitableview
.
并不是说我得到了用户配置文件的字段名称,而不是配置文件的值。
我想从这些数据动态生成表单。
我能够获取这些数据,NSMutableArray
但在cellForRowAtIndexPath
方法中它显示为空。
我该如何解决这个问题?
我的代码片段如下。
-(void) connectionDidFinishLoading:(NSURLConnection *)connection {
if (connection)
{
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
//You've got all the data now
//Do something with your response string
// NSLog(@"Response:%@",responseString);
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *object = [parser objectWithString:responseString error:nil];
NSString *pec_count = [object valueForKey:@"peculiarity_count"];
NSDictionary *pecs = [object valueForKey:@"peculiarities"];
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:nil];
[array addObject:@""];
for (int j= 1; j <= [pec_count integerValue] ; j++) {
NSString *val = [NSString stringWithFormat:@"%@%d",@"pec_",j];
NSString *pec_i = [pecs valueForKey:val];
NSString *modifiedString = [pec_i stringByReplacingOccurrencesOfString:@"_" withString:@" "];
NSString *capitalisedSentence = [modifiedString stringByReplacingCharactersInRange:NSMakeRange(0,1)
withString:[[modifiedString substringToIndex:1] capitalizedString]];
[array insertObject:capitalisedSentence atIndex:j];
}
self.peculiarity = array;
[self.table reloadData];
}
for (int j=0 ; j < [self.peculiarity count] ; j++) {
NSLog(@"info:%@", [self.peculiarity objectAtIndex: j]);
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIButton *racebtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
racebtn.frame = CGRectMake(240, 7, 10, 15);
[racebtn setBackgroundImage:[UIImage imageNamed:@"select.png"] forState:UIControlStateNormal];
[racebtn addTarget:self action:@selector(selectRace:)forControlEvents:UIControlEventTouchUpInside];
NSLog(@"cell=%@",[self.peculiarity objectAtIndex:3]);
}
任何帮助将不胜感激。谢谢你。