![](/img/trans.png)
[英]IOS (Objective-C) How to indefinitely change the image in a UITableView Cell from an Array of UIImages
[英]Objective-C, iOS SDK, How to save UITableView Cell image checkmark
我正在嘗試保存單元格的圖像狀態,以防止用戶退出應用程序或離開視圖時仍保存圖像。 我在想NSUserDefaults,最好的方法是什么? 謝謝。
目前我有這個:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell.imageView.image == [UIImage imageNamed:@"checkboxfull.png"]){
cell.imageView.image = [UIImage imageNamed:@"checkboxblank.png"];
}else if(cell.imageView.image == [UIImage imageNamed:@"checkboxblank.png"])
{
cell.imageView.image = [UIImage imageNamed:@"checkboxfull.png"];
// [alert show];
}
}
這實際上取決於要保存其內容的單元格數量。 如果很少,那么在NSUserDefaults中就沒什么大不了的,但是如果更多,那么Core Data可能是您更好的解決方案。
如果要使用NSUserDefaults,則可能要在其中設置BOOL以指示該復選框處於打開還是關閉狀態(因為只有兩個狀態)。 所以:
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"someUniqueIdentifier"];
希望這可以幫助。
我可能會使用NSUserDefaults進行一些操作,但不會單獨保存它們。 我可能會將每個單元格存儲在一個數組中。 這將使保存多個單元格變得更加容易。
NSMutableArray *checkState = [[NSArray alloc] init];
if([cell.imageView.image highlighted]) {
[checkState addObject:[NSNumber numberWithBool:YES]];
} else
[checkState addObject:[NSNumber numberWithBool:NO]];
Then when you load your table:
NSArray cellState = [NSUserDefaults standardDefaults] objectForKey:@"yourSavedKey"];
[cell.imageView.image setHighlighted:[cellState objectAtIndex:indexPath.row]];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.