Отображение свойства расстояния в ячейке таблицы, округленной до двух десятичных знаков, и показ суффикса в Km - PullRequest
0 голосов
/ 30 декабря 2010

У меня есть следующий код, показывающий объекты местоположения из NSArray в таблице, где каждый индекс NSArray представлен ячейкой в ​​таблице в моем классе SecondViewController.m:

    - (UITableViewCell *)tableView:(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;
}

Мой соответствующий код в моем классе LocationTableViewCell.m выглядит так:

UILabel *rDistance = [[UILabel alloc] initWithFrame:CGRectMake(250, 0, 250, 20)];

lDistance.font = [UIFont systemFontOfSize: 13]; lDistance.numberOfLines = 0; [self.contentView addSubview: lDistance]; locationDistance = lDistance; [Дистанционное освобождение];

В настоящее время я получаю свойство расстояния, отображаемое со многими десятичными знаками. Свойство distance находится в Km, поэтому я хочу, чтобы оно было округлено до двух десятичных знаков, а к метке добавлялся суффикс «Km» с пробелом между свойством distance и «Km». Как бы я это сделал?

1 Ответ

0 голосов
/ 30 декабря 2010

Проверьте следующий код

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;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...