Fabio
Fabio

Reputation: 3067

Can't dynamically create my custom cells on UITableView

I'm trying to create a UITableView in a UIViewController (I'd use UITableViewController but I want the UITableView to only use half of the screen space) that will create a new custom cell every time I press a button, and display the current time in that cell and the time since the last time I pressed the button (so the time in this cell minus the time in the cell above it). The problem is, when I press the button, either nothing happens or, if I don't initialise the property I created for the UITableView, a blank cell appears.

My cell has 3 UILabel's in it, inOut, entryTime and delta, and the IBOutlet I created for the UITableView is timeTable. newEntry is the button. I have set my UITableView's datasource and delegate to my UIViewController, have "called" the protocols UITableViewDataSource, UITableViewDelegate on my UIViewController's header. I can't find what's wrong.

here's my code for the UIViewController:

    @interface MainViewController ()

@property (strong, nonatomic) Brain *brain;
@end

@implementation MainViewController{
    @private
    NSMutableArray *_entryArray;
    NSMutableArray *_timeSinceLastEntryArray;
}
@synthesize brain=_brain;
@synthesize timeTable=_timeTable;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _brain = [[Brain alloc] init];
    _entryArray = [[NSMutableArray alloc] init];
    _timeSinceLastEntryArray = [[NSMutableArray alloc] init];
    _timeTable = [[UITableView alloc] initWithFrame:CGRectZero];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

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


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

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

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    if (indexPath.row%2==0){
        cell.inOut.text = [NSString stringWithFormat:@"Entrada %d", indexPath.row/2 +1];
        cell.entryTime.text = [NSString stringWithFormat:@"%@", [_entryArray objectAtIndex:indexPath.row]];
        if (indexPath.row==0){
            cell.delta.text=@"";
        }
        else {
            cell.delta.text = [_timeSinceLastEntryArray objectAtIndex:indexPath.row];
        }
    }
    else{
        cell.inOut.text = [NSString stringWithFormat:@"Saída %d", (indexPath.row+1)/2];
        cell.entryTime.text = [NSString stringWithFormat:@"%@",[_entryArray objectAtIndex:indexPath.row]];
        cell.delta.text = [_timeSinceLastEntryArray objectAtIndex:indexPath.row];
    }



    return cell;
}



- (IBAction)newEntry:(id)sender {

    [_entryArray addObject:[self.brain currentTime]];
    NSInteger numberOfEntrys= [_entryArray count];
    if (numberOfEntrys >1){
        NSString *timeSinceLastEntry = [_brain timeSince:[_entryArray objectAtIndex:(numberOfEntrys-2)]];
        [_timeSinceLastEntryArray addObject:timeSinceLastEntry];
    }
    else {
        [_timeSinceLastEntryArray addObject:@"0"];
    }

    [_timeTable reloadData];
}


@end

and for CustomCell:

@implementation CustomCell

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

@synthesize  inOut=_inOut, entryTime=_entryTime, delta=_delta;
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {

    if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {

        // Initialization code

        _inOut = [[UILabel alloc]init];

        _inOut.textAlignment = UITextAlignmentLeft;


        _entryTime = [[UILabel alloc]init];

        _entryTime.textAlignment = UITextAlignmentLeft;


        _delta = [[UILabel alloc]init];

        _delta.textAlignment = UITextAlignmentLeft;

        [self.contentView addSubview:_entryTime];

        [self.contentView addSubview:_inOut];

        [self.contentView addSubview:_delta];

    }

    return self;

}
@end

Thanks for the help :)

Upvotes: 0

Views: 784

Answers (1)

Zoon Nooz
Zoon Nooz

Reputation: 5866

I see you put the initialize code in

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier

But you create object with

cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

and you did not provided the Frame to the view that you created

_inOut = [[UILabel alloc]init];
_entryTime = [[UILabel alloc]init];
_delta = [[UILabel alloc]init];

Hope it helps you

Upvotes: 1

Related Questions