2015-10-29 16 views
0

我有一個簡單的UITableViewController,其中一些靜態單元格在UITableView中。 UITableView正在填充另一個UITableViewControllerprepareForSegue。此UITableViewController有兩個部分,其中第一部分是NSMutableArray字符串,第二部分是NSMutableArray,其中包含帶字符串的.txt文件。一些「語言」只有一個部分,其他部分有兩個。關於UITableView沒有什麼別的。使用本教程:http://www.appcoda.com/swipeable-uitableviewcell-tutorial/#disqus_thread,我已經設法在單元格上實現自定義手勢,所以我可以將單元格滑過以將其標記爲最喜歡的。我不得不執行該教程中使用的代碼來改變從刪除星默認行爲,可以在下面的截圖中可以看出:發生事件時更改特定UITableViewCell的UIImage

enter image description here

一旦用戶選擇了明星形象,我希望在單元格上存在UIImage,表示該單元格已被加星標。

下面是一些代碼,開始與cellForRowAtIndexPath

static NSString *strCellIdentifier = @"CustomLeafletVideoCell"; 
CustomLeafletVideoTableViewCell *customCell = (CustomLeafletVideoTableViewCell *)[tableView dequeueReusableCellWithIdentifier:strCellIdentifier]; 

self.rightUtilityButton = [NSMutableArray new]; 

if (![[NSUserDefaults standardUserDefaults] boolForKey:@"Favourite"]) 
{ 
    [self.rightUtilityButton sw_addUtilityButtonWithColor: 
    [UIColor colorWithRed:1.0f green:0.0f blue:0.0f alpha:1.0] 
               icon:[UIImage imageNamed:@"[email protected]"]]; 

} 
else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"Favourite"]) 
{ 
    [self.rightUtilityButton sw_addUtilityButtonWithColor: 
    [UIColor colorWithRed:1.0f green:0.0f blue:0.0f alpha:1.0] 
               icon:[UIImage imageNamed:@"[email protected]"]]; 


} 

customCell.rightUtilityButtons = self.rightUtilityButton; 
customCell.delegate = self; 

當用戶在小區水龍頭的圖像,此方法激發關閉:

- (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index 
{ 

    switch (index) { 
     case 0: 
     { 

     self.selectedRowCollection = [[NSMutableDictionary alloc] init]; 


     NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 

     CustomLeafletVideoTableViewCell *cell = (CustomLeafletVideoTableViewCell*)[self.tableView cellForRowAtIndexPath:indexPath]; 

     // We create a new NSString and assign it to the custom label's text (which is obtained from the cellForRow method) 
     NSString *cellTitle = cell.customCellLabel.text; 

     NSLog(@"The text is %@", cellTitle); 


     NSManagedObjectContext *context = [self managedObjectContext]; 

     Favourites *favourites = [NSEntityDescription insertNewObjectForEntityForName:@"Favourites" inManagedObjectContext:context]; 
     favourites.title = cellTitle; 

     NSError *error = nil; 
     if (![context save:&error]) 
     { 
      // Error 
     } 


     [cell hideUtilityButtonsAnimated:YES]; 


     [self.tableView reloadData]; 
    } 
} 

我存儲「標題」的核心數據。

問題

當我刷卡的單元格,按星圖像,它不會在指定的區域加載與UIImageView細胞。其原因不是因爲我真的不明白該做什麼。

我明白答案是指出我應該創建一個自定義類,並且當我理解背後的理論時,我無法理解如何正確地實現它。

我已經看過此參考文獻:Using BOOL objects in NSMutableArray to determine cell statuses使用選中標記作爲附件。這是有效的,但它會在對應的單元格的第一部分和第二部分中放置複選標記。

我已經想通了,我可以從indexPath.rowindexPath.section得到確切的行號和節號,但我不知道如何處理這些信息。我總是可以添加一個isFavouriteBOOLCore Data型號,但實際上,我怎麼設置,在cellForRowAtIndexPath?

因此得出結論,併爲250賞金我把關於這個問題,我需要了解:

  • 設置具體indexPath.rowindexPath.section與圖像,並
  • 維護它,這樣我每次回到這個UITableViewController,明星是在相應的單元格

我在這裏是一個新手,所以如果你很樂意儘可能地幫助儘可能多的信息,我會非常感激。

此外,我沒有問題與收藏之後做什麼;這是有效的 - 我只需要在單元格被標記爲收藏時將圖像放到單元格上。

任何指導,將不勝感激。

更新

這是我的代碼有設置收藏夾:

 self.isFavourite = @(!self.isFavourite.boolValue); 

self.isFavourite關於這個UITableViewController創建一個NSNumber屬性。

cellForRowAtIndexPath

customCell.customCellLabel.text = [NSString stringWithFormat:@"%@",[self.availableLeaflets objectAtIndex:indexPath.row]]; 
    if (self.isFavourite.boolValue) 
    { 
     customCell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 
    else 
    { 
     customCell.accessoryType = UITableViewCellAccessoryNone; 
    } 

代碼在這裏從self.availableLeaflets設置標題。這用於設置單元格上的複選標記,但問題是,當我回到此UITableViewController時,複選標記未保留在所選單元格上。

這裏,self.availableLeaflets是一個NSMutableArray,它從前面的UITableViewController中的prepareForSegue填充和設置。

回答

2

當用戶點擊一個單元格中的星形時,您的代碼將所有單元格標記爲收藏夾,因爲您在UserDefaults中只存儲了一個鍵。

您必須單獨存儲每個單元格的最愛狀態。每個單元格都必須有它自己的「最喜歡的」布爾值。

更新:

這是如何實現想不想使用核心數據的工作示例。爲簡明起見,我簡化了示例:

  1. tableview顯示單元格的標題。如果一個單元格是最喜歡的,它會顯示覆選標記附件視圖。
  2. 要標記單元格,只需選擇它。 (我省略了整個按鈕代碼,保持簡單的例子)。如果您選擇單元格第二次是從收藏

取出下面是你必須做的:

#import "ViewController.h" 
#import <CoreData/CoreData.h> 
#import "CellData.h" 
#import "AppDelegate.h" 

@interface ViewController() <UITableViewDataSource, UITableViewDelegate> 
@property (nonatomic) UITableView *tableView; 
@property (nonatomic) NSMutableDictionary *favoritesDict; 
@property (nonatomic) NSInteger context; 
@property (nonatomic) NSString *language; 
@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // setup the table view 
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds]; 
    self.tableView.dataSource = self; 
    self.tableView.delegate = self; 
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"]; 
    [self.view addSubview:self.tableView]; 

    // setup the favorites dictionary in user defaults (if it does not exist) 
    if ([[NSUserDefaults standardUserDefaults] valueForKey:@"favoritesDict"] == nil) { 
     [[NSUserDefaults standardUserDefaults] setObject:@{} forKey:@"favoritesDict"]; 
    } 

    // set the language of your current table view 
    self.language = @"en"; 

    // load favorites from user defaults 
    self.favoritesDict = [[[NSUserDefaults standardUserDefaults] valueForKey:@"favoritesDict"] mutableCopy]; 

} 

// this method changes the favorite state of the cell at a given index path 
- (void)toggleFavoriteStateForCellAtIndexPath:(NSIndexPath *)indexPath { 

    // toggle the favorite state of the cell 
    NSString *key = [NSString stringWithFormat:@"%@_%ld_%ld", self.language, (long)indexPath.section, (long)indexPath.row]; 
    if (self.favoritesDict[key] == nil) { 
     self.favoritesDict[key] = @(1); 
    } else { 
     [self.favoritesDict removeObjectForKey:key]; 
    } 

    // save the change 
    [[NSUserDefaults standardUserDefaults] setObject:self.favoritesDict forKey:@"favoritesDict"]; 

    // reload the cell 
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
} 

#pragma mark - UITableViewDataSource 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 2; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 10; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 

    // add the text to your cell 

    // set up favorite state 
    NSString *key = [NSString stringWithFormat:@"%@_%ld_%ld", self.language, (long)indexPath.section, (long)indexPath.row]; 
    if (self.favoritesDict[key]) { 
     // show the favorite image 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; // for this example: show checkmark 
    } else { 
     // hide the favorite image 
     cell.accessoryType = UITableViewCellAccessoryNone; // for this example: hide checkmark 
    } 

    return cell; 
} 

#pragma mark - UITableViewDelegate 

// toggle the favorite state of the cell when the user selects it 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    [self toggleFavoriteStateForCellAtIndexPath:indexPath]; 
} 

@end 

此示例爲具有多個部分表。它使用NSDictionary並將關鍵單元存儲在具有以下語法的鍵中LANGUAGE_SECTION_ROW。要檢查一個單元格是否被標記爲收藏夾,只需檢查字典中是否存在相關鍵的對象。該密鑰的實際價值並不重要。你只需檢查鑰匙。

只要確保在viewDidLoad中設置了語言字符串。

+0

謝謝@joern - 這是有道理的。請問我能否提供一些示例代碼來幫助我瞭解如何做到這一點?我想如果我在自定義單元格中創建了一個名爲favorite的BOOL,然後在這個類的cellForRowAtIndexPath中,我可以通過customCell.favourite引用它,但是如果將其設置爲YES,是不是也將所有單元格標記爲最喜歡的?我試圖找到一種方法來獲得indexPath.row? – amitsbajaj

+0

您不能將該布爾值放入您的自定義單元類中,因爲單元格沒有被系統持久保存,滾動時會重新使用該單元格。但是你的表格視圖有一個數據源來保存單元格的數據(比如爲每個單元顯示的文本)。你必須在那裏添加布爾值。 – joern

+0

謝謝@joern - 這是有道理的,我得到我應該把代碼放在cellForRowAtIndexPath代碼是定義單元格中的文本。我不太確定的是如何在代碼中做到這一點;如何使用indexPath設置選定的單元格。 – amitsbajaj

相關問題