I have fielded quite a few questions on the two-column table I have in the Data View page. Here’s how I did it:
NSMutableArray *datapoints = [self.data objectForKey:@"datapoints"];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@”date” ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
datapoints = [[datapoints sortedArrayUsingDescriptors:sortDescriptors] mutableCopy];
[sortDescriptor release];
[sortDescriptors release];
int count = 10;
if ([datapoints count] < 10)
{
count = [datapoints count];
}
int y = 180;
int x = 7;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
for (int i = 0; i < count; i++)
{
if (i == 5)
{
x = 167;
y = 180;
}
UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y+1, 100, 20)];
valueLabel.text = [[datapoints objectAtIndex:i] objectForKey:@”value”];
valueLabel.tag = 312;
valueLabel.font = [UIFont systemFontOfSize:15];
valueLabel.backgroundColor = [UIColor clearColor];
valueLabel.textColor = [UIColor colorWithHue:0 saturation:0 brightness:.2 alpha:1];
valueLabel.shadowColor = [UIColor whiteColor];
valueLabel.shadowOffset = CGSizeMake(0, 1);
[self.view addSubview:valueLabel];
[valueLabel release];
UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(x+97, y+4, 50, 15)];
dateLabel.text = [dateFormatter stringFromDate:[[datapoints objectAtIndex:i] objectForKey:@”date”]];
dateLabel.font = [UIFont systemFontOfSize:12];
dateLabel.textAlignment = UITextAlignmentRight;
dateLabel.tag = 312;
dateLabel.textColor = [UIColor colorWithHue:0 saturation:0 brightness:.4 alpha:1];
dateLabel.shadowColor = [UIColor colorWithWhite:1.0 alpha:1.0];
dateLabel.shadowOffset = CGSizeMake(0,1);
dateLabel.backgroundColor = [UIColor clearColor];
[self.view addSubview:dateLabel];
[dateLabel release];
y = y+29;
}
[dateFormatter release];
Just a simple for loop, with some clever resetting of x/y values. Easy!
