gpt4 book ai didi

ios - 保存UICollectionView的索引路径以在ActionSheet中使用

转载 作者:行者123 更新时间:2023-12-01 16:44:12 25 4
gpt4 key购买 nike

我在使用indexPath的UICollectionView中使用一个数组,并且我想在ActionSheet中使用具有相同indexPath的相同数组。是否可以通过任何方式保存indexPath并将其加载到actionSheet中?

这是我正在使用的代码(当然,我在操作表中使用了更多代码,并且可能可以正常工作,我只是将代码附加到出现问题的地方):

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
UILabel *label = (UILabel *)[cell viewWithTag:100];
UIImageView *favoriteImage = (UIImageView *)[cell viewWithTag:750];
// UIImageView *image = (UIImageView *)[cell viewWithTag:500];

//Sound title
label.text = [mainArray objectAtIndex:indexPath.row];

//Single sound image
// image.image = [UIImage imageNamed: [NSString stringWithFormat:@"%@.png", [array objectAtIndex:indexPath.row]]];

// Removes background color in cells
cell.backgroundColor = [UIColor colorWithRed:190.0/255 green:179.0/255 blue:59.0/255 alpha:0.0];



if ([FavoriteArray containsObject: [mainArray objectAtIndex:indexPath.row]])
{
favoriteImage.alpha = 1;
}




return cell;
}

- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
//Get the name of the current pressed button
NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:@"Destructive Button"]) {
if (self.favoriteAcitve == YES) {


if ([FavoriteArray containsObject: [mainArray objectAtIndex:indexPath.row]]) // YES
{
[FavoriteArray removeObject:[mainArray objectAtIndex:indexPath.row]];
[FavoriteArray writeToFile:listPath atomically:YES];

} else {
[FavoriteArray addObject:[mainArray objectAtIndex:indexPath.row]];
[FavoriteArray writeToFile:listPath atomically:YES];
NSLog(@"Count %i", [FavoriteArray count]);

// }}

NSLog(@"Destructive pressed --> Delete Something");
}
if ([buttonTitle isEqualToString:@"Other Button 1"]) {
NSLog(@"Other 1 pressed");
}
if ([buttonTitle isEqualToString:@"Other Button 2"]) {
NSLog(@"Other 2 pressed");
}
if ([buttonTitle isEqualToString:@"Other Button 3"]) {
NSLog(@"Other 3 pressed");
}
if ([buttonTitle isEqualToString:@"Cancel Button"]) {
NSLog(@"Cancel pressed --> Cancel ActionSheet");
}
}

最佳答案

如果要存储变量,请尝试以下操作:

使用NSUserDefaults,代码如下所示:

存储索引路径:

[[NSUserDefaults standardUserDefaults] setObject:indexPath.row forKey:@"nameForStoredVariableHere_row"];
[[NSUserDefaults standardUserDefaults] setObject:indexPath.sectionforKey:@"nameForStoredVariableHere_section"];

[[NSUserDefaults standardUserDefaults] synchronize];

检索存储的索引路径:
 NSNumber *storedRow = [[NSUserDefaults standardUserDefaults] objectForKey:@"nameForStoredVariableHere_row"];
NSNumber *storedSection = [[NSUserDefaults standardUserDefaults] objectForKey:@"nameForStoredVariableHere_section"];

之后,您可以随意使用indexPath

关于ios - 保存UICollectionView的索引路径以在ActionSheet中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21464694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com