7

I am having trouble with a UITableViewCell.

I have a view controller in my storyboard that is connected to my view controller called MainViewController. It Contains a UITableViewCell with 3 labels. The UITableViewCell is connected to the class MTTableViewCell.

// MTTableViewCell.h

#import <UIKit/UIKit.h>

@interface MTTableViewCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *mainLabel;
@property (strong, nonatomic) IBOutlet UILabel *statusLabel;

@end

// MTTableViewCell.m

#import "MTTableViewCell.h"

@implementation MTTableViewCell

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

@end

// MainViewController.m

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;        
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    MTTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[MTTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }        

    NSString *namecell = @"namecell";
    NSString *statuscell = @"statuscell";           
    [cell.mainLabel setText:namecell];
    [cell.statusLabel setText:statuscell];        

    return cell;
}

The problem is that nothing is shown when I run the MainViewController. What am I missing? I am not getting any arrors, just an empty tableview with 1 blank record.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
Arnout
  • 657
  • 4
  • 12
  • 23
  • Have you set up outlets correctly? – Lorenzo B Nov 14 '13 at 09:11
  • Make sure that cellForRowAtIndexPath is called. Maybe you missed some outlets or delegates – Grzegorz Krukowski Nov 14 '13 at 09:18
  • Make sure to connect the table 'delegate' property in your storyboard. Also you have to put some data into the table. You have to specify number of sections and number of rows in each section. – Nat Nov 14 '13 at 09:29
  • The outlets are set correctly. The function cellForRowAtIndexPath is called. The table 'delegate' property is currently connected to the MainViewController file's owner – Arnout Nov 14 '13 at 10:08
  • 1
    This reference on Custom Table Cells is much better than Apple's. http://www.apeth.com/iOSBook/ch21.html#_custom_cells Also, if you are bashing your head over why views and subview hierarchies are not rendering in UITableView or UICollection view one good way to debug these issues is to remove affected .xib files from Xcode, only deleting the reference, and then add them back. Often when sniping and cutting IBOutlets, the nibs can get into a bad state with broken references even if no error displays. Interface Builder will happily show and say nothing. One of the 8 wonders of Xcode.! – jcpennypincher Apr 22 '15 at 18:03

10 Answers10

2

Look in your interface builder - you shuld have not "conntected" your custom cell, you should set it as a "files owner" since you implemented the view, not want to set a delegate. Maybe this hint helps?

ThorstenC
  • 1,264
  • 11
  • 26
  • How do you mean? So I do not need to connect the delegate outlet to MainViewController? – Arnout Nov 14 '13 at 10:09
  • For your 'MTTableViewCell' you need to set it as a file owner of your custom class. Look in utility area in the files inspector and try to set "Custom Class" to "MTTableViewCell" when the graphical cell is selected. – ThorstenC Nov 14 '13 at 11:30
  • I solved it. I was using interface MainViewController : UIViewController while it should have been just interface MainViewController : UIViewController – Arnout Nov 14 '13 at 13:06
  • @Arnout It's unlikely the above fixed it. The reason is probably that the Restoration ID property in the designer wasn't matching the reusable cell identifier. – Candide Jun 08 '14 at 09:55
2

You are not loading cell from main Bundle : Use Below code this will work for you :

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = @"FleetAccountCell";

FleetAccountCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
    cell = [[[NSBundle mainBundle] loadNibNamed:@"FleetAccountCell" owner:nil options:nil] objectAtIndex: 0];;
}
    return cell;
}
Alok
  • 24,880
  • 6
  • 40
  • 67
1

Try looking here ... the answer to your problem could be      

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

     CustomCell *cell = (CustomCell * )[self.tableView dequeueReusableCellWithIdentifier:@"YOUR CELL NAME" forIndexPath:indexPath];

   // your content cell...


     return cell;
 }

Read it here PFQueryTableViewController not showing custom cells

Greetings :)

Community
  • 1
  • 1
kAiN
  • 2,559
  • 1
  • 26
  • 54
1

This work for me..

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = @"cellID";

FleetAccountCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
    cell = [[[NSBundle mainBundle] loadNibNamed:@"Your_nibName" owner:nil options:nil] objectAtIndex: 0];
}
[cell layoutIfNeeded];
return cell;
}
imattuz
  • 51
  • 3
1

Declare TableViewcell this way :

TableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

Below one was not working for me :

TableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
Vineet Singh
  • 4,009
  • 1
  • 28
  • 39
garg
  • 2,651
  • 1
  • 24
  • 21
0

Rewrite you cellForRowAtIndexPath: method as below

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";

        MTTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"CUSTOME_CELL_Nib_NAME" owner:nil options:nil] objectAtIndex: 0];;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }        

        NSString *namecell = @"namecell";
        NSString *statuscell = @"statuscell";           
        [cell.mainLabel setText:namecell];
        [cell.statusLabel setText:statuscell];        

        return cell;
    }
Suryakant Sharma
  • 3,852
  • 1
  • 25
  • 47
0
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    TableViewCell *cell = (TableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell==nil)
    {
            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil];
            cell = [topLevelObjects objectAtIndex:0];
    }
    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
Ajay.km
  • 117
  • 2
  • 11
0

I had the same problem, I was missing a connection in the tableview delegates and datasource:

self.yourtableviewname.delegate=self;
self.youtableviewname.datasource=self;

After that it worked fine.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Preetha
  • 753
  • 9
  • 12
0

Select the cell and in the inspector tab,check mark "installed" and it will work.

-1

You didn't instantiate mainLabel and statusLabel,so they are nil right now.You must instantiate them in the initWithStyle method.

Hunter
  • 136
  • 1
  • 9
  • mainLabel and statusLabel are obviously already on the view - he connected it by using outlets. So he don't need no implicit initialize them? – ThorstenC Nov 14 '13 at 13:13