1

我正在开发一个 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]);

}

任何帮助将不胜感激。谢谢你。

4

3 回答 3

0

我之前已经检查过上述所有解决方案,最后我得到了解决方案。问题是我的手机在数据到达之前就被加载了。所以我使用了对服务器的同步请求。

代码如下:

  NSString *path = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"path"];

NSString *address = [NSString stringWithFormat:@"%@%@%@%@", path,@"users/",@"peculiarity/",self.tablename];

NSURL *URL = [NSURL URLWithString:address];
NSLog(@"%@",address);

[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[URL host]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLCacheStorageAllowedInMemoryOnly
                                                   timeoutInterval:60.0];

[request setHTTPMethod:@"GET"];


NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

if (data) {

    NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"data:%@",responseString);

    SBJsonParser *parser = [[SBJsonParser alloc] init];
    NSDictionary *object = [parser objectWithString:responseString error:nil];

    pecs = [object valueForKey:@"pec_values"];

    for (int j =0; j < [pecs count] ; j++) {

           NSLog(@"values:%@",[pecs objectAtIndex:j]);
    }


    self.peculiarity = array;

}
else {
    // Handle error by looking at response and/or error values
    NSLog(@"%@",error);
}
于 2013-08-13T05:48:23.770 回答
0

在 connectionDidFinishLoading: 方法中替换以下行:

self.peculiarity = 数组;和,

self.peculiarity = [[NSMutableArray alloc] initWithArray:array];

并在 cellForRowAtIndexPath 方法中添加以下代码行:

cell.textLabel.text = [self.peculiarity objectAtIndex:indexPath.row];

希望这会帮助你。

于 2013-08-01T10:59:55.617 回答
0

注意:如果您没有分配self.peculiarity NSMutableArray,那么像下面这样分配该数组..

    if (self.peculiarity == nil)
        self.peculiarity = [[NSMutableArray alloc] init];

然后将该数组分配给该数组,如下所示..

self.peculiarity = array;

在 incellForRowAtIndexPath:方法之后,只需将该值或名称设置为textLabelOf UITableViewCelllike bellow..

   cell.textLabel.text = [self.peculiarity objectAtIndex:indexPath.row];

使用该方法查看整个示例..

- (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];
        [cell addSubview:racebtn];
        cell.textLabel.text = [self.peculiarity objectAtIndex:indexPath.row];
     }
     return cell;
}
于 2013-08-01T10:37:01.417 回答