简体   繁体   中英

custom cell not appear in tableview

I use storyboards. I create new class for my Cell, in storyboard I write in Reuse Identifier "userFullCell". My Cell class .h

#import <UIKit/UIKit.h>

@interface UsefullLinksCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UIImageView *image;
@property (weak, nonatomic) IBOutlet UILabel *shortTitle;

@end

.m default

#import "UsefullLinksCell.h"

@implementation UsefullLinksCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
  self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  if (self) {
    // Initialization code
}
return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
  [super setSelected:selected animated:animated];

// Configure the view for the selected state
}

property and connected in storyboard with UILabel and UIImageView in Cell 在故事板中与Cell中的UILabel和UIImageView连接

Then in my class with UITableView I import "UseFullLinksCell.h" and in viewDidLoad wrote:

[self.tableView registerClass:[UsefullLinksCell class] forCellReuseIdentifier:@"userFullCell"];

tableView is my outlet:

@property (weak, nonatomic) IBOutlet UITableView *tableView;

and then I try to create cells:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath
{
  static NSString *CellIdentifier = @"userFullCell";
  UsefullLinksCell *cell = (UsefullLinksCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  NSDictionary *info = resourceArray[indexPath.row];
  if(info){
    cell.image.image = info[@"logo"];
    cell.shortTitle.text = info[@"title"];
  }
  return cell;
}

So, cell initialize, but image and text not assigned, and after going end of method return 0x0000000 for cell. If I create for standart cell code in this method, I mean UITableViewCell *cell, there is all good. Where I could make mistake? PS Sorry I can not give screenshots from storyboard, because I write from not my computer, if you want, I provide images later

You should register the xib file (with registerNib:forCellReuseIdentifier:) where you made your custom cell, not the class. Also, I don't know if this will make a difference or not, but replace this:

UsefullLinksCell *cell = (UsefullLinksCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

With this (notice the forIndexPath: at the end of the method):

UsefullLinksCell *cell = (UsefullLinksCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

After Edit:

If added a custom cell to your table view in the storyboard, and gave it the identifier "userFullCell", then you don't need to register anything -- in fact, registering your class makes it not work. So, just comment out that register statement, and see if that works.

You have missed something:

  NSArray *objects=[[NSBundle mainBundle]loadNibNamed:@"UsefullLinksCell" owner:self options:nil];
 UsefullLinksCell *cell =[objects objectAtIndex:0];

this will give you the object of your custom cell.

I think you have to do the following

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
 {
     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
     if (self) {
      // Initialization code

          image=[[UIImageView alloc] initWithFrame:CGRectMake(0,0,50.0,50.0)];
          shortTitle=[[UILabel alloc] initWithFrame:CGRectMake(60.0,0.0,200.0,50.0)];
     }
     return self;
}

Else all should work.. The thing is your components are not initialized anywhere, either it should be loaded from xib or, you have to initialize them manually.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM