开发者

Updating tableview cells

I'm still really green as Object-C so hopefully I'm able to communicate this well, so hopefully with my code examples I can explain this.

What I have is a table with four rows. Each row has a label (Clock in time, out to lunch time, etc) and a time (detailTextLabel). The label's are stored in an array and the detail's are generated from the NSDate family of functions (please forgive my terminology). In order to change the time values I use a data picker adjusted to pick time. An action is used to update the detail of a row when the time is changed using the picker.

The follow code snippets are in the same *.m class.

Here's a snipped down version of this -

// textLabel goes on the left, detailTextLabel goes on the right
// This puts the labelArray on the left and times on the right
- (UITableViewCell *)tableView:(UITableView *)tableView cel开发者_高级运维lForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *kCustomCellID = @"CustomCellID";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCustomCellID];

    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kCustomCellID] autorelease];

        // This disable the hightlighter effect when we select a row.
        // We need the highlighter, but we'll leave the code here.
        // cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    cell.textLabel.text = [self.labelArray objectAtIndex:indexPath.row];


    // --- Start of routine to work with adding hours and minutes to stuff

    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];

    // --- Set Clock In time (initially time 'now').  
    if (indexPath.row == [labelArray indexOfObjectIdenticalTo:@"Clock In"])
    {
        self.clockIn = [NSDate date];

        cell.detailTextLabel.text = [self.dateFormatter stringFromDate:clockIn];

        NSLog(@"Clock In - %@", clockIn);
        //NSLog(@"Clock In (cell*) - %@", cell.detailTextLabel.text);

        return cell;
    }    

    // --- Set Out to Lunch time (initially clockIn + 5 hours)
    if (indexPath.row == [labelArray indexOfObjectIdenticalTo:@"Out to Lunch"])
    {
        [offsetComponents setHour:5];

        self.outToLunch = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]
                              dateByAddingComponents:offsetComponents toDate:[NSDate date] options:0];

        cell.detailTextLabel.text = [self.dateFormatter stringFromDate:outToLunch];

        NSLog(@"indexPath.row (Out to Lunch): %i", indexPath.row);

        NSLog(@"Out to Lunch - %@", outToLunch);
        //NSLog(@"Out to Lunch (cell*) - %@", cell.detailTextLabel.text);

        return cell;
    }

    // Leaving out two other if clauses as they duplicate the Out to Lunch clause.

    //cell.detailTextLabel.text = [self.dateFormatter stringFromDate:[NSDate date]];
    //return cell;
    return nil;

This chunk of code work well and gives me no issues.

When a row, such as "Clock In" is selected an animiation is called that scroll's up a time picking datePicker. As the times are scrolled by the "Clock In" time updates.

Here's where I have my problem. I can't figure out how to have the "Out to Lunch" row update as the "Clock In" time is updated.

Here's the code I have for my picking action -

- (IBAction)dateAction:(id)sender
{
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    cell.detailTextLabel.text = [self.dateFormatter stringFromDate:self.pickerView.date];

    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];

    if (indexPath.row == [labelArray indexOfObjectIdenticalTo:@"Clock In"])
    {
        if (cell.detailTextLabel.text != [self.dateFormatter stringFromDate:clockIn])
        {
            NSLog(@"clockIn time changed...");

            // Since clockIn time changed, we need to change outToLunch, inFromLunch, and clockOut

            // Change the outToLunch time
            [offsetComponents setHour:5];

            self.outToLunch = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]
                               dateByAddingComponents:offsetComponents toDate:self.pickerView.date options:0];

            NSLog(@"New Out to Lunch time: %@", [self.dateFormatter stringFromDate:outToLunch]);

            // Here is where I get stuck
        }
        //return nil;
    }
}

What I envision for the "Change the outToLunch time" clause is something like this...

  1. Take the new clockIn time and add 5 hours to it, and make this the new outToLunch time. a. NSLog just to see that this math function worked.
  2. Using the index of the detailTextLabel for the outToLunch time cell, put this new time there.

There will be two more rows along with the outToLunch row that will be updated at the same time.

How can I do this?

Thanks for your help.


at the end of your - (IBAction)dateAction:(id)sender use the following:

    // Here is where I get stuck
      }
      //return nil;
  }
  NSArray *indexPathArray=[NSArray arrayWithObject:indexPath];
  [self.tableView reloadRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationNone];
}

alternatively, you could use [self.tableView reloadData], but that is overkill for just reloading a single row, especially if the number of rows you have is large.


After working on this with timthetoolman's helpful advice and lots of variations of Googling. I found this stackoverflow article: " How to select the indexPath of the UITableView after a pop?" that Thomas answered of which I used the following code pieces:

Added to .h file

    NSIndexPath* outToLunchIndexPath;

        //...
    }

    //...

    @property (nonatomic, retain) NSIndexPath* outToLunchIndexPath;

    //...

    @end

Added to .m file

@synthesize outToLunchIndexPath;

Then down in my code I added these lines

self.outToLunch = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]
                           dateByAddingComponents:offsetComponents toDate:self.pickerView.date options:0];
        // New code...
        UITableViewCell *outToLunchCell = [self.tableView cellForRowAtIndexPath:outToLunchIndex];
        outToLunchCell.detailTextLabel.text = [self.dateFormatter stringFromDate:outToLunch];

        NSLog(@"New Out to Lunch time: %@", [self.dateFormatter stringFromDate:outToLunch]);

This had the affect of updating the contents of the cell.detailLabel.text of the original space as the clockIn time changed with the datePicker.

A big thank you to timthetoolman and Thomas for their help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜