2011-05-16 51 views
0

作爲Object-C,我仍然很綠,所以希望我能夠很好地溝通,希望能夠用我的代碼示例來解釋這一點。更新tableview單元

我所擁有的是一張有四行的表格。每行都有一個標籤(時間,時間,午餐時間等)和時間(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的滾動條。隨着時間的推移,「時鐘進入」時間更新。

這是我的問題所在。我無法弄清楚如何在更新「時鐘輸入」時間時更新「外出到午餐」行更新。

下面的代碼我有我的採摘行動 -

- (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. 採取新clockIn時間併爲其添加5個小時,並將其作爲新的outToLunch時間。 a。 NSLog只是爲了看到這個數學函數的工作。
  2. 對outToLunch時間單元格使用detailTextLabel的索引,將這個新時間放在那裏。

將會有兩行以及outToLunch行將同時更新。

我該怎麼做?

感謝您的幫助。

回答

0

在與timthetoolman的工作有關的建議和谷歌搜索的變化很多之後。我發現這個計算器的文章:「How to select the indexPath of the UITableView after a pop?」那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時間改變了更新原有空間的cell.detailLabel.text內容的影響datePicker。

非常感謝timthetoolmanThomas的幫助。

1

在你結束 - (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],但是這是矯枉過正只是一重裝單行,特別是如果您擁有的行數很大。

+0

謝謝,但我必須錯過一段代碼,因爲當我點擊選取輪三次後,包含您的線條時,應用程序崩潰。我已經縮小到重新加載部分。這是短暫的崩潰錯誤:***由於未捕獲的異常'NSInvalidArgumentException',原因:'*** - [NSArray initWithObjects:count:]:試圖在對象[0]處插入nil對象'*** I我也在研究這個錯誤信息。 – Reuben 2011-05-17 00:21:33

+0

在創建indexPathArray之前檢查您的indexPath是否爲零。它可能在重新加載行時,行變爲未選中,導致indexPath爲零,並導致reloadRowsAtIndexPath方法拋出異常。 保持選中的行可能不是最好的UI練習。或者,您可以在didSelectRowAtIndexPath方法中設置一個實例變量,如mySelectedIndexPath。然後你可以使用NSArray * indexPathArray = [NSArray arrayWithObject:self.mySelectedIndexPath];從上面的代碼。 – timthetoolman 2011-05-17 02:10:22

+0

你也應該釋放一些你已經分配過的對象。即NSDateComponents * offsetComponents = [[NSDateComponents alloc] init]; 運行靜態分析器將幫助您識別這樣的事情。 – timthetoolman 2011-05-17 02:21:07