Oscar Apeland
Oscar Apeland

Reputation: 6662

Label not properly implemented in UITableView, why?

I have a tableView which should be populated with an NSMutableArray. This worked fine without a custom prototype UITableViewCell. I have an own file for the cell, named SimpleTableCell. The prototype cell in my storyboard has this class assigned to it. Contents of SimpleTableCell.h:

#import <UIKit/UIKit.h>

@interface SimpleTableCell : UITableViewCell
@property (nonatomic, weak) IBOutlet UILabel *nameLabel;
@property (nonatomic, weak) IBOutlet UILabel *moreInfoLabel;
@end

This is imported to the viewController where I have the tableView, like this: CargoViewController.h

#import <UIKit/UIKit.h>
#import "StartupViewController.h"
#import "boatInfoViewController.h"
#import "SimpleTableCell.h"

@interface CargoViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate, UITableViewDelegate, UIPickerViewDataSource>

@property(strong,nonatomic) IBOutlet UILabel *testLabel; //label I use to test stuff instead of LOG
@property(strong,nonatomic) IBOutlet UIButton *findOwner; //button to summon pickerWheel
@property(strong,nonatomic) IBOutlet UIButton *findShip; //Button to confirm selection in picker and unhide tableView
@property(strong,nonatomic) NSArray *boatOwners; //Array for boat owners.
@property(strong,nonatomic) NSMutableArray *boatsForOwner; //array for boats, added by owner
@property(strong,nonatomic) IBOutlet UITableView *boatsTableView; //Table view
-(IBAction)findOwnerButtonPressed:(id)sender;
-(IBAction)findShipButtonPressed:(id)sender;
@property(strong, nonatomic) IBOutlet UIPickerView *shipOwners; //ship owner picker wheel
@property(strong,nonatomic)IBOutlet UILabel *infoLabel;
/*@property(strong,nonatomic)IBOutlet UILabel *nameLabel; //These was tested, but didn't work. Illegal assignment error.
@property(strong,nonatomic)IBOutlet UILabel *moreInfoLabel;
@property(strong,nonatomic)IBOutlet UITableViewCell *SimpleTableCell;*/

@end

This, again, controls to the CargoViewController.m (Only relevant parts added)

#import "CargoViewController.h"
#import "SimpleTableCell.h"
@interface CargoViewController ()


@end

@implementation CargoViewController
@synthesize testLabel, findOwner, findShip,shipOwners, boatsTableView, infoLabel;
@synthesize boatOwners, boatsForOwner;

//When I try to synthesize name and infolabel in cell here, it does not work

//...more code...

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [boatsForOwner count];
}

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
    cell = [[SimpleTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}

cell.nameLabel.text = [boatsForOwner objectAtIndex:indexPath.row];
return cell;
}

My problem is: cell.nameLabel.text is not found on object UITableViewCell Which, according to enter image description here my storyboard is wrong(?)

For further information, the table worked before I implemented prototype cells. Before I just used defaults. Error trigger:

#pragma mark -
#pragma mark PickerView DataSource

- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return [boatOwners count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView
         titleForRow:(NSInteger)row
        forComponent:(NSInteger)component
{
return [boatOwners objectAtIndex:row];
}


#pragma mark -
#pragma mark PickerView Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
  inComponent:(NSInteger)component
{
NSString *boatsFromOwner = [boatOwners objectAtIndex:[shipOwners selectedRowInComponent:0]];
//if(boatsForOwner) {[boatsForOwner release]; boatsForOwner = nil;}
boatsForOwner = [[NSMutableArray alloc] init];
if ([boatsFromOwner isEqualToString:@"Owner1"]) {
    [self.boatsForOwner addObject:@"Titanic1"];
    [self.boatsForOwner addObject:@"Titanic2"];
    [self.boatsForOwner addObject:@"Titanic3"];
    [self.boatsForOwner addObject:@"Titanic4"];
    [self.boatsForOwner addObject:@"Titanic5"];
    [self.boatsForOwner addObject:@"Titanic6"];
    [self.boatsForOwner addObject:@"Titanic7"];

}
NSLog(@"%@",boatsFromOwner);

[boatsTableView reloadData];

}

Upvotes: 1

Views: 180

Answers (2)

tkanzakic
tkanzakic

Reputation: 5499

change this line:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

for this:

SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

and by the way, when using storyboard tableView:dequeueReusableCellWithIdentifier: will never return nil.

Upvotes: 1

iuser1969782
iuser1969782

Reputation: 14

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

try change to

SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

Upvotes: 0

Related Questions