2013-05-09 76 views
0

我從服務器使用php腳本獲取問題。我根據問題的數量設置單元格的數量,但是當我寫入單元格時,它只輸出1個問題。如果我使用for循環,單元格是空白的,但是如果我設置了數字,它會根據數據庫中有多少個問題重複相同的問題。下面是代碼:如何設置表格目標中的每個單元格的文本c

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];是你告訴劇本你想要檢索什麼問題的地方。

+1

你在哪裏添加此代碼? – Mohith 2013-05-09 07:18:38

+2

如果這在cellForRowAtIndexPath中,則除去for循環並添加_appendString = [[NSNumber numberWithInt:indexPath.row] stringValue]; – Mohith 2013-05-09 07:19:29

+0

它在cellForRowAtIndexPath中,但沒有奏效,單元格仍然空白 – heinst 2013-05-09 07:25:30

回答

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填充使你的表光滑。目前你的桌子表現不平。

+0

是的,我當然同意它滯後......非常感謝 – heinst 2013-05-09 07:55:43

相關問題