0

我仍然非常喜欢 Object-C,所以希望我能够很好地沟通这一点,所以希望通过我的代码示例我可以解释这一点。

我有一张有四行的桌子。每行都有一个标签(打卡时间、午餐时间等)和一个时间(detailTextLabel)。标签存储在一个数组中,细节是从 NSDate 系列函数生成的(请原谅我的术语)。为了更改时间值,我使用了一个调整为选择时间的数据选择器。当使用选择器更改时间时,操作用于更新行的详细信息。

以下代码片段在同一个 *.m 类中。

这是一个剪断的版本 -

// 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 cellForRowAtIndexPath:(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;

这段代码运行良好,没有给我带来任何问题。

When a row, such as "Clock In" is selected an animiation is called that scroll's up a time picking datePicker. 随着时间的滚动,“时钟”时间更新。

这就是我遇到问题的地方。随着“打卡”时间的更新,我不知道如何更新“外出吃午饭”行。

这是我的挑选操作的代码 -

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

我对“更改 outToLunch 时间”条款的设想是这样的......

  1. 取新的时钟时间并增加 5 小时,并将其设置为新的 outToLunch 时间。一个。NSLog 只是为了查看这个数学函数是否有效。
  2. 使用 outToLunch 时间单元格的 detailTextLabel 的索引,将这个新时间放在那里。

将同时更新两行以及 outToLunch 行。

我怎样才能做到这一点?

谢谢你的帮助。

4

2 回答 2

1

在您的 - (IBAction)dateAction:(id)sender 末尾使用以下内容:

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

或者,您可以使用 [self.tableView reloadData],但这对于仅重新加载单行来说太过分了,尤其是在您拥有的行数很大的情况下。

于 2011-05-17T00:02:37.137 回答
0

在与timthetoolman的有用建议和谷歌搜索的许多变体一起工作之后。我找到了这篇stackoverflow文章:“如何在弹出后选择UITableView的indexPath?托马斯回答了我使用以下代码片段:

添加到 .h 文件

    NSIndexPath* outToLunchIndexPath;

        //...
    }

    //...

    @property (nonatomic, retain) NSIndexPath* outToLunchIndexPath;

    //...

    @end

添加到 .m 文件

@synthesize outToLunchIndexPath;

然后在我的代码中我添加了这些行

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]);

这具有更新原始空间的 cell.detailLabel.text 内容的影响,因为时钟时间随着 datePicker 的变化而变化。

非常感谢timthetoolmanThomas的帮助。

于 2011-05-17T23:54:24.430 回答