[英]How do I align text in an array in a table view?
我是Objective-C編碼的新手。 但是,如果您看一下我的輸出下面的圖像,我的問題非常簡單。 基本上,我希望所有內容都正確對齊,以使行中的所有數據看起來都像在自己的列中一樣。 基本上,每一行都應與其上方的行對齊。 我的程序正在從Web的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;
}
除了HTML解決方案,我看不到其他方法。
只需使用表創建NSString並將其顯示在UIWebView中。
例如
NSString* htmlString = [NSString stringWithFormat:@"<table><tr>"
"<td>%@</td>"
"</tr></table>",
JSON_VARIABLE];
[webView loadHTMLString:htmlString baseURL:nil];
在這里代替空格:
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]];
您可以決定每個字符串將具有的標准長度並用空格字符填充所需的額外空間。如果您在所有字符串之間選擇最大長度,則不會剪切任何字符串:
NSString* formatted=[yourString stringByPaddingToLength: maxLength withString: @" " startingAtIndex: 0];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.