[英]Updating tableview cells
我仍然像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;
这段代码工作良好,没有任何问题。
当选择诸如“ Clock In”之类的行时,将调用动画,该滚动使时间选择datePicker向上滚动。 随着时间的推移,“ Clock In”时间更新。
这是我的问题所在。 随着“时钟进入”时间的更新,我不知道如何更新“午餐”行。
这是我选择操作的代码-
- (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时间”子句的设想是这样的...
将有两行以及outToLunch行将同时更新。
我怎样才能做到这一点?
谢谢你的帮助。
在-(IBAction)dateAction:(id)发件人的末尾使用以下命令:
// Here is where I get stuck
}
//return nil;
}
NSArray *indexPathArray=[NSArray arrayWithObject:indexPath];
[self.tableView reloadRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationNone];
}
或者,您可以使用[self.tableView reloadData],但这对于仅重新加载单行来说是多余的,尤其是当您拥有的行数很大时。
在使用timthetoolman的有用建议和Googling的许多变体进行此工作之后。 我找到了这篇stackoverflow文章:“ 如何在弹出后选择UITableView的indexPath? ” Thomas回答了其中,我使用了以下代码段:
已添加到.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]);
当clockIn时间随datePicker更改时,这具有更新原始空间的cell.detailLabel.text内容的效果。
非常感谢timthetoolman和Thomas的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.