0

I want to have custom cell in UITableView, I create my UIView via storyboard and I linked them to the code

I don't know why my picture does not appear

Would you please check my code ?

customise code .h

: UITableViewCell

@property (strong, nonatomic) IBOutlet UIImageView *weekImg;



@end

customise code .m

@synthesize weekImg;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    // Initialization code
    weekImg=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"red.png"]];

}
return self;
}

My method: cellForRowAtIndexPath:

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

 WeekTableViewCell *cell = (WeekTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"WeekTableViewCell"];



NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];

[[cell textLabel] setText:contentForThisRow];

  return cell;
 }
  • In order for us to be able to answer your question, you need to add some more information. Right now there isn't enough detail included for anyone to do much more than guess :) More specifically, what circumstances did the process terminate under, what's the backtrace, etc. – Dustin Aug 07 '12 at 12:19

2 Answers2

0

I think this is because your code is just looking for previosuly created cells. at th start no cells will be created and wont be able to retrieve any this way.

WeekTableViewCell *cell = (WeekTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"WeekTableViewCell"];


if (cell == nil){
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"WeekTableViewCell" owner:nil options:nil];
        for(id currentObject in topLevelObjects)
        {
            if([currentObject isKindOfClass:[WeekTableViewCell class]])
            {
                cell = (WeekTableViewCell *)currentObject;

                break;
            }
        }
    }
 cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];

[[cell textLabel] setText:contentForThisRow];

return cell;

this way create a new cell if there isnt a cell to re use

glogic
  • 4,042
  • 3
  • 28
  • 44
  • thanks but terminated on this line :: NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"WeekTableViewCell" owner:nil options:nil]; –  Aug 07 '12 at 12:19
  • do you have a nib file that is your custom table cell? if not try creating a nib file that is of type tableviewcell, give it the name WeekTableViewCell, give it the identifier WeekTableViewCell. then you can add the uiimageview to the nib file and not need to add it in code like you have above. while not the only solution this is just how i have done it in the past – glogic Aug 07 '12 at 12:22
  • and be sure to hook the WeekTableViewCell code file with the WeekTableViewCell nib file – glogic Aug 07 '12 at 12:24
  • ah yes my bad afraid i cant help you then , have no experience with storyboards as of yet. http://stackoverflow.com/questions/9245969/in-a-storyboard-how-to-make-a-custom-cell-for-use-with-multiple-controllers maybe this read will help – glogic Aug 07 '12 at 12:29
  • I fixed that but picture doesn't appear would you please check it –  Aug 07 '12 at 12:42
  • maybe change weekImg=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"red.png"]]; to weekImg.image = [UIImage imageNamed:@"red.png"]; since you are already creating and linking the weekImg in the storyboard and really all you want to do is change its image not re initialise it. once again im just guessing at this point as ive never worked with storyboards – glogic Aug 07 '12 at 12:45
0
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.imageView.layer.cornerRadius=10.0f;
    [cell.imageView.layer setMasksToBounds:YES];
    cell.textLabel.text=[NSMUtableArray objectAtIndex:indexPath.row];
switch (indexPath.row)
        {
            case 0:
                cell.imageView.image=[UIImage imageNamed:@"img1.png"];
                break;
            case 1:
                cell.imageView.image=[UIImage imageNamed:@"img2.png"];
                break;
            case 2:
                cell.imageView.image=[UIImage imageNamed:@"img3.png"];
                break;
            case 3:
                cell.imageView.image=[UIImage imageNamed:@"img4.png"];
                break;
            default:
                break;
        }
}




    cell.imageView.layer.cornerRadius=10.0f;
    [cell.imageView.layer setMasksToBounds:YES];, it is used by QuartzCore.

The output of the cell like this, we can adjust the image size.

enter image description here

Vineesh TP
  • 7,755
  • 12
  • 66
  • 130