Вставка изображения в ячейку UITableView - PullRequest
1 голос
/ 10 ноября 2011

извините за глупый вопрос, но я все еще изучаю программирование на Objective-C и iPhone.

Я пытаюсь вставить значок слева от ячейки UITableView. Это из метода (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath:

NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"icon3" ofType:@"png"];
UIImage *icon = [UIImage imageWithContentsOfFile: imagePath];
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
cell.imageView.image = icon;

Моя иконка (30w x 44h) ДОЛЖНА быть такой же высокой, как и ячейка, в то время как на данный момент остается небольшой пробел (возможно, 1 пиксель) над ней и ниже . Я попытался изменить размеры изображения безуспешно и попытался уменьшить высоту ячейки (без эффекта) с помощью (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath.

Что я могу сделать?

В конце концов мне тоже нужно вставить немного места слева, но это второстепенно.

Большое спасибо.

This is the screenshot: check that white pixel above the yellow icon

[ОБНОВЛЕНИЕ] нет проблем, когда UITableView не «сгруппирован»

Ответы [ 3 ]

3 голосов
/ 10 ноября 2011

Вам нужно изменить высоту строки, чтобы изменить размер cell.imageView.

реализовать следующий метод и вернуть высоту вашего изображения.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

Попробуйте такжеудаление параметра contentMode в вашем методе cellForRowAtIndexPath.Вы можете заменить весь свой код одной строкой, как показано ниже:

cell.imageView.image = [UIImage imageNamed:@"icon3.png"];
2 голосов
/ 10 ноября 2011

На основе кода из CanadaDev здесь :

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5,5,20,20)]; // set the location and size of your imageview here.
imageView.image = [UIImage imageNamed:@"image.png"]; // set the image for the imageview
imageView.highlightedImage = [UIImage imageNamed:@"imageHighlighted.png"]; // if you need it, this will allow you to create an image used when the cell is highlighted. For example, a white copy of your image to match the white text.
[cell addSubview:imageView];

и затем для текста:

UILabel *price=[[UILabel alloc]initWithFrame:CGRectMake(80,60, 180,12)];
price.text=[NSString stringWithFormat:@"price %@",temp];
price.backgroundColor=[UIColor clearColor];
[price setFont:[UIFont fontWithName:@"American Typewriter" size:12]];
[cell.contentView addSubview:price];
[price release];
0 голосов
/ 10 ноября 2011

Попробуйте настроить размер cell.imageView.

cell.imageView.bounds = CGRectMake(0,0,0,0) //fill in appropriate location and size for the zeros
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...