2010-10-29 2 views
0

Я использую это вКак добавить изображение во все ячейки в UITableView?

- (UITableViewCell *)tableView:(UITableView *)atable cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    UIImage *image = [[UIImage alloc] imageNamed:@"arrow.jpg"]; 
    cell.imageView.image = image; 
    return cell; 
} 

cell.imageView.image бросает ошибку ..

+0

Я объявляю ячейку ... Это всего лишь часть кода .... – Cintu

ответ

0

Я предполагаю, что вы имеете в виду ошибки во время выполнения, потому что код, как есть, не будет компилировать (потому что клетка не объявляется).

Эта линия:

UIImage *image = [[UIImage alloc] imageNamed:@"arrow.jpg"]; 

должно быть:

UIImage *image = [UIImage imageNamed:@"arrow.jpg"]; 

imageNamed является метод класса UIImage, а не метод экземпляра.

Edit:
Как вы просили, чтобы поместить изображение справа, а не слева, вы можете попробовать это:

UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow.jpg"]]; 
cell.accessoryView = iv; 
[iv release]; 

Если расположение клеток требует больше гибкости, чем то, что встроенная, вам нужно будет добавить свои собственные представления в cell.contentView или просто создать пользовательскую ячейку в IB.

+0

- (UITableViewCell *) tableView: (UITableView *) atable cellFor RowAtIndexPath: (NSIndexPath *) indexPath { \t static NSString * CellIdentifier = @ "Cell"; \t UITableViewCell * cell = [atable dequeueReusableCellWithIdentifier: CellIdentifier]; \t if (cell == nil) { \t \t cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: CellIdentifier] autorelease]; \t} \t [[изображение изображения в ячейке] setImage: [UIImage imageNamed: @ "arrow.png"]]; \t NSString * cellValue = [listOfItems objectAtIndex: indexPath.row]; \t [cell.textLabel setText: cellValue]; \t \t обратная ячейка; } – Cintu

+0

[[изображение изображения в ячейке] setImage: [UIImage imageNamed: @ "arrow.png"]]; – Cintu

+0

Теперь это работает .. но изображение выравнивается влево, мне нужно в праве что делать? – Cintu

0

Я использовал этот подход в игре, я развиваюсь.

Подкласс UITableViewCell, затем вы добавляете UIImageView и UILabel в ячейку программно. это дает вам отличный способ настроить внешний вид вашей камеры.

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier { 
    if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) { 
     // Initialization code 
     headerLabel = [[UILabel alloc]init]; 
     headerLabel.textAlignment = UITextAlignmentLeft; 

     imageV = [[UIImageView alloc] init]; 

     descriptionLabel.font = [UIFont fontWithName:@"Marker Felt" size:14]; 
     descriptionLabel = [[UILabel alloc]init]; 
     descriptionLabel.textAlignment = UITextAlignmentLeft; 
     descriptionLabel.font = [UIFont fontWithName:@"Marker Felt" size:11]; 
     descriptionLabel.lineBreakMode = UILineBreakModeWordWrap; 
     descriptionLabel.numberOfLines = 0; 

     [self.contentView addSubview:headerLabel]; 
     [self.contentView addSubview:descriptionLabel]; 
     [self.contentView addSubview:imageV]; 
    } 
    return self; 
} 

- (void)layoutSubviews { 
    [super layoutSubviews]; 
    CGRect contentRect = self.contentView.bounds; 
    CGFloat boundsX = contentRect.origin.x; 
    CGRect frame; 

    frame= CGRectMake(boundsX+60 ,3, 200, 15); 
    headerLabel.frame = frame; 

    frame= CGRectMake(boundsX+60 ,18, 260, 39); 
    descriptionLabel.frame = frame; 

    frame= CGRectMake(boundsX+3 , 3, 54, 54); 
    imageV.frame = frame; 
} 

Кроме того, вам придется изменить метод в классе UITableView:

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSString *CellIdentifier = @"Cell"; 

    AchievementCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[AchievementCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    [cell returnCellAtIndex:indexPath.row]; 
    [cell returnImageAtIndex:indexPath.row]; 

    return cell; 
} 

Не стесняйтесь задавать больше вопросов относительно кода :)

0

использование этого ..

- (UITableViewCell *)tableView:(UITableView *)atable cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

cell.imageView.image = [UIImage imageNamed:@"arrow.jpg"]; 
return cell; 

} 
Смежные вопросы