简体   繁体   中英

UILabel text in non-reusable UITableView cell can't be set

I decided to use non-reusable, custom UITableView cells in my project because I run a timer in each cell and updating one cell's timer with reusable cells resets all timers. Actually, the timers are running in my view controller but the output is displayed in the cell. But this question is far more basic: why doesn't the text in a UILabel in the cell get updated? I have set 2 outlets in the IB: nameLabel and timeLabel, and connected them to the cell's header file, and IB shows the outlets connected. However, even trying to move a literal string into the UILabel text field results in 'nil.' I suspect I am doing something basically wrong but I can't figure it out. There is no problem when I use a reusable cell. Here is the part of the code that allocates and initializes a mutable array to hold the cells, adds a cell, then tries to move the literal string into it. Setting a breakpoint at the NSLog statement shows that cell.nameLabel.text is 'nil.' Any help would be greatly appreciated! I have read the UITableViewCell reference and the programming guide but they only discuss using reusable cells. I think setting reuseIdentifier to 'nil' gets a non-reusable cell but I'm not sure.

    -(IBAction)addNewItem:(id)sender
{
   // get a new item.
    NRCItem *newItem = [[NRCItemStore sharedStore] createItem];

    // set timerType to 1 (seconds) - default
    newItem.timerType = 1;

    // get a timer controller
    NRCTimerControllerViewController *timerViewController = [[NRCTimerControllerViewController alloc]init];

    // set timer controller's item
    timerViewController.item = newItem;

    // get a new tableViewCell

    if(!_myTableViewCells){
        _myTableViewCells = [[NSMutableArray alloc]init];
    }

        NRCItemCell *cell = [[NRCItemCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
        cell.nameLabel.text =@"cell";
        NSLog(@"addTableViewCell cell= %@", cell);

        [_myTableViewCells addObject:cell];

    // push the timerViewController onto the navigation controller's stack
    [self.navigationController pushViewController:timerViewController animated:YES];

}

enter code here

I think I was not setting a delegate in the xib file. The code is now working.

    - (IBAction)addNewItem:(id)sender
{
    // control comes here when a new item is requested by the user, for example.
    // by clicking on a button in the view's navigation bar


    NRCtimerItem *item = [[NRCItemStore sharedStore]createItem];


    NRCDetailViewController *controller = [[NRCDetailViewController alloc]init];
    self.timerItem = item;
    self.timerItem.timerSet = NO;
    self.timerItem.resetHistory = [[NSMutableArray alloc]init];
    // Give detail view controller a pointer to the item object in row
    controller.item = item;
    controller.timerLabels = self.timerLabels;
    controller.delegate = self;

    // Push it onto the top of the navigation controller's stack
    [self.navigationController pushViewController:controller
                                         animated:YES];

    // add the row to tableView
    NSUInteger row =[[[NRCItemStore sharedStore]allItems] indexOfObject:item];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    self.timerItem.timerSet = YES;
}

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Get a new or recycled cell
    NRCItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NRCItemCell" forIndexPath:indexPath];

    NRCtimerItem *item = [[[NRCItemStore sharedStore]allItems]objectAtIndex:indexPath.row];
    self.timerItem = item;
    if(self.timerItem.timerSet ==NO){
        return cell;
    }
    // ******************************
    else{

    [self handleTimer];

    return cell;
    }
}
    -(void)handleTimer{

    if(self.timerItem.timerSet == NO)
    {
    self.timerItem.timerSet = YES;
        if(self.timerItem.timerReset == NO)
        {
            self.timerItem.startTime = [NSDate date];
            //self.timerItem.lastReset = [NSDate date];
            NSLog(@"Setting current date/time to %@", self.timerItem.startTime);
        }
    switch (self.timerItem.timerType) {
        case 0:// date only
        {
            self.timerItem.interval = 0.5;
        }
            break;
        case 1:// seconds
        {
            NSTimeInterval timeInterval = 1;
            self.timerItem.interval = timeInterval;
        }
            break;
        case 2:// minutes
        {
            NSTimeInterval timeInterval = 60;
            self.timerItem.interval = timeInterval;
        }
            break;
        case 3:// hours
        {
            NSTimeInterval timeInterval = 3600;
            self.timerItem.interval = timeInterval;
        }
            break;
        case 4:// days
        {
            NSTimeInterval timeInterval = 3600*24;
            self.timerItem.interval = timeInterval;
        }
            break;
        case 5:// months
        {
            NSTimeInterval timeInterval = 3600*24*30;
            self.timerItem.interval = timeInterval;
        }
            break;
        case 6:// years
        {
            NSTimeInterval  timeInterval = 3600*24*365;
            self.timerItem.interval = timeInterval;

        }
            break;
        }
            NSLog(@"Date/time was set to %@", self.timerItem.startTime);

            NSTimeInterval interval =[self.timerItem.startTime timeIntervalSinceNow];
            interval = (-1 * interval);
            if(self.timerItem.interval && self.timerItem.interval >= interval){
                self.timerItem.interval = self.timerItem.interval - interval;
            }
        }


    //NSLog(@"timer %@ fired", self.timerItem.timerName);

    // here we schedule the timer to fire after an interval based on the timerType.
    self.timerItem.timer = [NSTimer scheduledTimerWithTimeInterval:self.timerItem.interval target:self selector:@selector(calculateTimer:) userInfo:self.timerItem repeats:YES];



    [self.timerItem.timer fire];


}

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