0

我正在使用 php 脚本从服务器获取问题。我正在根据问题的数量设置单元格的数量,但是当我写入单元格时,它只输出 1 个问题。如果我使用 for 循环,单元格是空白的,但如果我设置数字,它会根据数据库中有多少问题重复相同的问题。继承人的代码:

    NSString *numOfQuestionsURL = @"http://**.***.**.**/count.php";
    NSData *dataURLforSize = [NSData dataWithContentsOfURL:[NSURL URLWithString: numOfQuestionsURL]];
    NSString *serverOutputforSize = [[NSString alloc] initWithData:dataURLforSize encoding:NSASCIIStringEncoding];
    int numOfQuestions = [serverOutputforSize intValue];
    for(int i = 0; i <= numOfQuestions; i++)
    {
        _hostStr = @"http://**.***.**.**/getQuestion.php?num=";
        _appendString = [[NSNumber numberWithInt:i] stringValue];
        _hostStr = [_hostStr stringByAppendingString: _appendString];
    }
    NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString: _hostStr]];
    NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding:NSASCIIStringEncoding];
    result.textLabel.text = serverOutput;

_appendString = [[NSNumber numberWithInt:i] stringValue]; 是您告诉脚本您要检索什么问题的地方。

4

1 回答 1

1

你需要像这样使用这段代码

- (NSInteger)tableView:(UITableView *)tableView 
 numberOfRowsInSection:(NSInteger)section {

    NSString *numOfQuestionsURL = @"http://**.***.**.**/count.php";
        NSData *dataURLforSize = [NSData dataWithContentsOfURL:[NSURL URLWithString: numOfQuestionsURL]];
        NSString *serverOutputforSize = [[NSString alloc] initWithData:dataURLforSize encoding:NSASCIIStringEncoding];
        return [serverOutputforSize intValue];
}





- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  //make cell

 _hostStr = @"http://**.***.**.**/getQuestion.php?num=";
        _appendString = [[NSNumber numberWithInt:indexPath.row] stringValue];
        _hostStr = [_hostStr stringByAppendingString: _appendString];
 NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString: _hostStr]];
    NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding:NSASCIIStringEncoding];
cell.textLabel.text = serverOutput;

  return cell;
}

您还可以在后台加载数据并维护一系列问题并填充到 table.that 使您的表格平滑。目前你的桌子表现得很生涩。

于 2013-05-09T07:51:42.290 回答