我有一個簡單的UITableViewController
,其中一些靜態單元格在UITableView
中。 UITableView
正在填充另一個UITableViewController
的prepareForSegue
。此UITableViewController
有兩個部分,其中第一部分是NSMutableArray
字符串,第二部分是NSMutableArray
,其中包含帶字符串的.txt文件。一些「語言」只有一個部分,其他部分有兩個。關於UITableView
沒有什麼別的。使用本教程:http://www.appcoda.com/swipeable-uitableviewcell-tutorial/#disqus_thread,我已經設法在單元格上實現自定義手勢,所以我可以將單元格滑過以將其標記爲最喜歡的。我不得不執行該教程中使用的代碼來改變從刪除星默認行爲,可以在下面的截圖中可以看出:發生事件時更改特定UITableViewCell的UIImage
一旦用戶選擇了明星形象,我希望在單元格上存在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.row
和indexPath.section
得到確切的行號和節號,但我不知道如何處理這些信息。我總是可以添加一個isFavourite
BOOL
到Core Data
型號,但實際上,我怎麼設置,在cellForRowAtIndexPath?
因此得出結論,併爲250賞金我把關於這個問題,我需要了解:
- 設置具體
indexPath.row
和indexPath.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
填充和設置。
謝謝@joern - 這是有道理的。請問我能否提供一些示例代碼來幫助我瞭解如何做到這一點?我想如果我在自定義單元格中創建了一個名爲favorite的BOOL,然後在這個類的cellForRowAtIndexPath中,我可以通過customCell.favourite引用它,但是如果將其設置爲YES,是不是也將所有單元格標記爲最喜歡的?我試圖找到一種方法來獲得indexPath.row? – amitsbajaj
您不能將該布爾值放入您的自定義單元類中,因爲單元格沒有被系統持久保存,滾動時會重新使用該單元格。但是你的表格視圖有一個數據源來保存單元格的數據(比如爲每個單元顯示的文本)。你必須在那裏添加布爾值。 – joern
謝謝@joern - 這是有道理的,我得到我應該把代碼放在cellForRowAtIndexPath代碼是定義單元格中的文本。我不太確定的是如何在代碼中做到這一點;如何使用indexPath設置選定的單元格。 – amitsbajaj