[英]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.