我仍然非常喜欢 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 时间”条款的设想是这样的......
- 取新的时钟时间并增加 5 小时,并将其设置为新的 outToLunch 时间。一个。NSLog 只是为了查看这个数学函数是否有效。
- 使用 outToLunch 时间单元格的 detailTextLabel 的索引,将这个新时间放在那里。
将同时更新两行以及 outToLunch 行。
我怎样才能做到这一点?
谢谢你的帮助。