user1832095
user1832095

Reputation: 65

How do I align text in an array in a table view?

I'm new to Objective-C coding. However, my problem is very straightforward if you take a look at the image below of my output. Basically I want everything to be aligned correctly such that all the data in the rows look like they are in their own columns. Basically each row should be aligned with the row above it. My program is bringing in each field from a JSON file from the web - please also see code to see how I'm doing it.

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];

My Table View:

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

Upvotes: 1

Views: 309

Answers (3)

Ramy Al Zuhouri
Ramy Al Zuhouri

Reputation: 21996

You can decide a standard length that every string will have and fill the extra space needed with spaces characters.If you choose the maximum length between all strings, then no string will be cut:

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

Upvotes: 0

pbibergal
pbibergal

Reputation: 2921

I don't see other way rather than HTML solution.
Simply create NSString with table and show it in UIWebView.

E.g.

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

Upvotes: 1

fiso
fiso

Reputation: 1415

Instead of blank spaces here:

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

You can use tabulator "\t", something like:

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

Upvotes: 0

Related Questions