2013-03-12 31 views
-1

嗨我是xcode的新手,我一直在尋找解決問題的答案。我有一個表格視圖,並且我有像任務列表一樣設置的複選標記。我想知道當我從視圖移動到視圖時,如何將檢查標記保存在我的任務列表中。現在,當我轉到應用程序中的另一個視圖時,當我回到表視圖控制器時,這些視圖都消失了。我沒有設置核心數據,也沒有使用播放列表。這裏的代碼很簡單:在表格視圖中保存選中標記

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

    if (cell.accessoryType == UITableViewCellAccessoryNone) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } else { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 
} 

檢查是否有一些簡單的代碼,當我回到表視圖時會使這個保存。

+1

你應該在你的下一篇文章中更好地格式化你的代碼。感謝vikingosegundo修復代碼格式。 – 2013-03-12 20:06:18

+0

您可以添加一個字段,如表中所選,並且如果該值被選中,那麼使用yes或1來更新它,無論您需要什麼值。當你回到tableview時,只是在viewWillAppear中從數據庫和cellforRowAtIndexPath中獲取數據,只需檢查該值是否被選中,然後將它們標記爲checked,否則取消選中。你也可以通過創建自定義單元來完成。 – 2013-03-13 05:21:38

+0

我有核心數據工作,它是從數組中拉我的數據。核心數據中有一個名爲checkMark的字段,設置爲一個字符串。如果它是真的,我想用它來保留我的複選標記。但是現在我不確定如何將數據從單元連接到核心數據,並在視圖加載後返回。再次感謝! – 2013-03-13 20:53:30

回答

0

如果你想保留應用程序重新啓動之間的複選標記狀態,最好的解決方案似乎使用核心數據。雖然它可能起初看起來有點矯枉過正,你試圖做什麼,它肯定會得到回報你的應用變得更加先進。您將擁有一個可靠的數據管理系統,您可以在其中進一步構建。
在這種情況下,您可能需要創建一個表示任務的實體,在最簡單的情況下,您只需要兩個屬性:NSString名稱和BOOL isCompleted。 當然,所有這些都可以使用簡單的XML,但從一開始就使用Core Data將是更有前途的證明。

你可以在這裏找到更多關於核心數據的信息。 http://developer.apple.com/library/mac/#documentation/cocoa/Conceptual/CoreData/cdProgrammingGuide.html

+0

好酷我不想走那麼遠,但我想我會不得不。從頭開始,我的頭腦很好。我已經通過幾個帶有文本框的教程,但是我只是添加了一個複選標記而沒有發現任何內容。謝謝!! – 2013-03-12 20:09:12

+0

您是否知道是否有包含核心數據和複選標記的教程? – 2013-03-13 13:09:22

+0

http://iosprogrammer.blogspot.be/2011/05/using-core-data-to-create-simple-to-do.html 本教程應該讓你走上正確的軌道,但它不提供很多解釋..因此,首先遵循http://www.raywenderlich.com/934/core-data-on-ios-5-tutorial-getting-started以更好地瞭解Core Data之前的工作原理開始構建您的應用程序。祝你好運! – s1m0n 2013-03-13 19:43:44

0

我已採取自定義複選框,並在的cellForRowAtIndexPath寫成:

 static NSString *CustomCellIdentifier = @"CustomCell"; 

     ReportAttendedBy *cell = (ReportAttendedBy *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier]; 

     if (cell == nil) 
     { 
      NSArray *nib; 
      nib = [[NSBundle mainBundle] loadNibNamed:@"ReportAttendedBy" owner:self options:nil]; 
      for(id oneObject in nib) 
       if([oneObject isKindOfClass:[ReportAttendedBy class]]) 
        cell = (ReportAttendedBy *)oneObject; 
     } 

     [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
     cell.textLabel.font= [UIFont fontWithName:@"Arial Rounded MT Bold" size:15.0]; 
     cell.textLabel.textColor=[UIColor whiteColor]; 

     ReportCreateOrView *repVw= [arrAttendedBy objectAtIndex:indexPath.row]; 
     cell.txtCustName.text = repVw.strCustomer; 
     cell.txtCustName.tag = indexPath.row + 100; 
     cell.txtCustName.delegate = self; 
     [cell.txtCustName addTarget:self action:@selector(goAway:) forControlEvents:UIControlEventEditingDidEndOnExit]; 

     [cell.btnCheckBox addTarget:self action:@selector(checkboxClicked:) forControlEvents:UIControlEventTouchUpInside]; 
     cell.btnCheckBox.tag = indexPath.row + 5000; 

     NSLog(@"repVw.strSelected=== %@",repVw.strSelected); 

     if([repVw.strSelected isEqualToString:@"1"]) 
     { 
      [cell.btnCheckBox.currentBackgroundImage isEqual:[UIImage imageNamed:@"ckeckbox_checked.png"]]; 

      // cell.txtCustName.userInteractionEnabled = true; 
     } 
     else 
     { 
      [cell.btnCheckBox.currentBackgroundImage isEqual:[UIImage imageNamed:@"ckeckbox_unchecked.png"]]; 
      //cell.txtCustName.userInteractionEnabled = false; 
     } 

     return cell; 

和我在checkboxClicked方法保持與1複選框值和零和以及改變按鈕的圖像

-(IBAction)checkboxClicked:(id)sender 
    { 
     NSLog(@"tag=== %d",[sender tag] - 5000); 

     int index= [sender tag] - 5000; 
     UIButton *btn= (UIButton *)sender; 

     ReportCreateOrView *repObj= [arrAttendedBy objectAtIndex:index]; 

     if([btn.currentBackgroundImage isEqual:[UIImage imageNamed:@"checkbox_unchecked.png"]]) 
     { 
      [btn setBackgroundImage:[UIImage imageNamed: @"checkbox_checked.png"] forState:UIControlStateNormal]; 
      selCustId = [repObj.strCustId intValue]; 
      val = 1; 
      [self updateAttendedBy]; //in this method i've written query to update value in table 

     } 
     else if([btn.currentBackgroundImage isEqual:[UIImage imageNamed:@"checkbox_checked.png"]]) 
     { 
      [btn setBackgroundImage:[UIImage imageNamed: @"checkbox_unchecked.png"] forState:UIControlStateNormal]; 
      selCustId = [repObj.strCustId intValue]; 
      val= 0; 
      [self updateAttendedBy]; 
     } 
    } 

並且不要忘記編寫viewWillAppear中的getvalues方法。 :)

+0

這看起來不錯,但在我剛剛使用故事板創建我所做的事情之前,我還沒有加載nib文件。對不起,我將不得不創建一個名爲ReportAttendedBy與表視圖的nib文件? – 2013-03-13 13:04:09

+0

是的。你必須讓類的UITableViewCell和.xib文件也引用這個類。 – 2013-03-15 04:15:47

相關問題