I have the following code showing location objects from an NSArray in a table, with each index of the NSArray represented by a cell in the table in my SecondViewController.m class:
- (UITableViewCell *)tableView开发者_如何学C:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"restaurantcell";
LocationTableViewCell *cell = (LocationTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[LocationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
Location *location = [locationList objectAtIndex:indexPath.row];
cell.locationName.text = location.name;
cell.locationAddress.text = location.address;
cell.locationDistance.text = location.distance;
return cell;
}
My relevant code in my LocationTableViewCell.m class looks like this:
UILabel *rDistance = [[UILabel alloc] initWithFrame:CGRectMake(250, 0, 250, 20)];
lDistance.font = [UIFont systemFontOfSize:13]; lDistance.numberOfLines = 0; [self.contentView addSubview:lDistance]; locationDistance = lDistance; [lDistance release];
At the moment, I am getting the distance property appearing with many decimal places. The distance property is in Km, so I want it to be rounded to two decimal places, and with the suffix "Km" appended to the label with a space between the distance property, and the "Km". How would I do this?
Check the following code
float distance = 120.354686;
NSString distanceString = [NSString stringWithFormat:@"%0.2f Km",distance];
UILabel *rDistance = [[UILabel alloc] initWithFrame:CGRectMake(250, 0, 250, 20)];
rDistance.text = distanceString;
精彩评论