1

我是 Objective-C 编码的新手。但是,如果您看一下我的输出下面的图像,我的问题就很简单了。基本上我希望所有内容都正确对齐,以便行中的所有数据看起来就像它们在自己的列中一样。基本上每一行都应该与它上面的行对齐。我的程序从网络上的 JSON 文件中引入每个字段 - 另请参阅代码以了解我是如何做到的。

在此处输入图像描述

-(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;
}
4

3 回答 3

1

我没有看到其他方式而不是 HTML 解决方案。
只需使用表格创建 NSString 并将其显示在 UIWebView 中。

例如

NSString* htmlString = [NSString stringWithFormat:@"<table><tr>"
                                                    "<td>%@</td>"
                                                    "</tr></table>",
                                                    JSON_VARIABLE];
[webView loadHTMLString:htmlString baseURL:nil];
于 2012-11-18T20:42:53.957 回答
0

您可以决定每个字符串将具有的标准长度,并用空格字符填充所需的额外空间。如果您选择所有字符串之间的最大长度,则不会剪切任何字符串:

NSString* formatted=[yourString stringByPaddingToLength: maxLength withString: @" " startingAtIndex: 0];
于 2012-11-18T20:46:36.223 回答
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]];
于 2012-11-18T20:18:40.707 回答