发生事件时更改特定UITableViewCell的UIImage

我有一个简单的UITableViewController ,在UITableViewController有一些静态单元格。 UITableView正由另一个UITableViewControllerprepareForSegue填充。 这个UITableViewController有两个部分,第一部分是字符串的NSMutableArray ,第二部分是包含带字符串的.txt文件的NSMutableArray 。 有些“语言”只有一个部分,有些则有两部分。 关于UITableView没什么特别的。 通过使用本教程: http : //www.appcoda.com/swipeable-uitableviewcell-tutorial/#disqus_thread ,我已经设法在单元格上实现自定义手势,因此我可以滑动单元格将其标记为collections夹。 我必须在该教程中实现代码的使用,以将默认行为从Delete更改为Star,如下面的屏幕截图所示:

在此处输入图像描述

一旦用户选择了星形图像,我希望在单元格上有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:@"Favorites@2x.png"]]; } 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:@"Database@2x.png"]]; } 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加载单元格。 它不是因为我真的不明白该怎么做。

我理解答案表明我应该创建一个自定义类,虽然我理解其背后的理论,但我无法理解如何恰当地实现它。

我已经看过这个参考: 在NSMutableArray中使用BOOL对象来确定单元格状态,以便在选择单元格时使用复选标记作为附件。 这是有效的,但它在第一部分和第二部分的同一对应单元格上打勾。

我已经发现我可以从indexPath.rowindexPath.section获得确切的行号和节号,但我只是不确定如何处理这些信息。 我总是可以在Core Data模型中添加一个isFavourite BOOL ,但是我如何在cellForRowAtIndexPath?实际设置它cellForRowAtIndexPath?

总而言之,对于我正在提出这个问题的250个赏金,我需要了解如何:

  • 使用图像设置特定的indexPath.rowindexPath.section
  • 保持它,以便每次我回到这个UITableViewController ,星星都在适当的单元格上

我在这里是个新手,所以如果你很乐意尽可能多地提供帮助,我真的很感激。

另外,我没有关于如何处理collections夹的问题; 这工作 – 当一个单元格被标记为collections时,我只需要将图像放到单元格上。

任何指导将不胜感激。

更新

这是我设置collections夹的代码:

  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 ,它从前一个UITableViewControllerprepareForSegue中填充和设置。

当用户点击一个单元格中的星号时,您的代码会将所有单元格标记为collections,因为您只在UserDefaults中存储了1个键。

您必须单独存储每个单元格的collections状态。 每个单元格必须具有自己的“最喜欢”布尔值。

更新:

这是一个关于如何在不使用Core Data的情况下实现您想要的实例。 为清晰起见,我简化了示例:

  1. tableview显示单元格的标题。 如果单元格是collections夹,则会显示复选标记附件视图。
  2. 要标记单元格,只需选择它即可。 (我省略了整个按钮代码,保持示例简单)。 如果您再次从collections夹中删除单元格

这是你要做的:

 #import "ViewController.h" #import  #import "CellData.h" #import "AppDelegate.h" @interface ViewController ()  @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的键中。 要检查单元格是否标记为collections夹,请检查字典中是否存在相关键的对象。 该密钥的实际值无关紧要。 你只需要检查密钥。

只需确保在viewDidLoad设置语言字符串。