tags:

views:

19

answers:

2

I am using this in

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

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

cell.imageView.image is throwing error..

A: 

I'm assuming you mean a runtime error because that code as-is won't compile (because cell is not declared).

This line:

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

should be:

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

imageNamed is a UIImage class method, not an instance method.

Edit:
As you requested, to put the image on the right instead of the left, you can try this:

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

If your cell layout requires more flexibility than what's built in, you'll need to add your own views to cell.contentView or just design a custom cell in IB.

aBitObvious
- (UITableViewCell *)tableView:(UITableView *)atable cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [atable dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; } [[cell imageView] setImage:[UIImage imageNamed:@"arrow.png"]]; NSString *cellValue = [listOfItems objectAtIndex:indexPath.row]; [cell.textLabel setText:cellValue]; return cell;}
Chinthan Kyasanur
[[cell imageView] setImage:[UIImage imageNamed:@"arrow.png"]];
Chinthan Kyasanur
Now it works.. but image is left aligned i need in right what to do?
Chinthan Kyasanur
I updated the answer.
aBitObvious
A: 

I used this approach in a game i an developing.

You subclass UITableViewCell, then you add a UIImageView and UILabel to the cell programatically. this gives you a great way to customize the look of your cell.

- (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;
}

Also you will have to change a method in the UITableView class:

// 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;
}

Feel free to ask any more questions regarding the code :)