2012-11-18 22 views
0

我是Objective-C編碼的新手。但是,如果您查看下面的輸出圖像,我的問題非常簡單。基本上我希望所有東西都能正確對齊,這樣行中的所有數據看起來就像是在自己的列中。基本上每行應該與它上面的行對齊。我的程序是從網絡的JSON文件中引入每個字段 - 請參閱代碼以瞭解我如何執行此操作。如何在表視圖中排列數組中的文本?

enter image description here

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 

    NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil]; 



    for (NSDictionary *diction in allDataDictionary) { 
     NSString *currDate = [diction objectForKey:@"Current Date"]; 
     NSString *timePeriod = [diction objectForKey:@"Time Period"]; 
     NSString *transCount = [diction objectForKey:@"Transaction Processed"]; 
     NSString *approved = [diction objectForKey:@"Approved"]; 
     NSString *posApproved = [diction objectForKey:@"Standing App"]; 
     NSString *approvalRate = [diction objectForKey:@"Approval Rate"]; 
     NSString *respTime = [diction objectForKey:@"Resp Time"]; 

     [array addObject:[NSString stringWithFormat:@"%@  %@  %@  %@  %@  %@", timePeriod, transCount, approved, posApproved, approvalRate, respTime]]; 



    } [[self myTableView] reloadData]; 

我的表視圖:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if(!cell) 
    { 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // NSString *currDate = [[array objectAtIndex:indexPath.row] objectForKey:@"Current Date"]; //added 
    // NSString *someOtherKey = [[array objectAtIndex:indexPath.row] objectForKey:@"Some other key"]; //added 


    cell.textLabel.text = [array objectAtIndex:indexPath.row]; 
    // cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", currDate, someOtherKey]; //added 

    return cell; 
} 

回答

0

代替空格這裏:

array addObject:[NSString stringWithFormat:@"%@  %@  %@  %@  %@  %@", timePeriod, transCount, approved, posApproved, approvalRate, respTime]]; 

您可以使用製表 「\ t」 的,是這樣的:

array addObject:[NSString stringWithFormat:@"%@ \t %@ \t %@ \t %@ \t %@ \t %@", timePeriod, transCount, approved, posApproved, approvalRate, respTime]]; 
+0

很高興知道,謝謝。你有什麼建議,我可以如何協調一切? – user1832095

+0

使用\ t分隔每個元素,如果文本的長度有些相似,它應該對齊文本,否則可以使用兩個標籤。 – Sandeep

+0

不幸的是,這將無法正常工作,因爲每個元素可能會有不同數量的數字,因爲他們進來 – user1832095

1

我沒有看到其他方式,而不是HTML解決方案。
只需用表格創建NSString並在UIWebView中顯示。

E.g.

NSString* htmlString = [NSString stringWithFormat:@"<table><tr>" 
                "<td>%@</td>" 
                "</tr></table>", 
                JSON_VARIABLE]; 
[webView loadHTMLString:htmlString baseURL:nil]; 
0

可以決定一個標準長度每個字符串都會有,並填寫與characters.If您選擇的所有字符串之間的最大長度的空間所需的額外空間,則沒有字符串將被削減:

NSString* formatted=[yourString stringByPaddingToLength: maxLength withString: @" " startingAtIndex: 0]; 
+0

我不確定這會去哪裏以及如何實現它 – user1832095

相關問題