gpt4 book ai didi

ios - 将 `UIImageView` 设置为不在自定义 UITableViewCell 上突出显示

转载 作者:行者123 更新时间:2023-11-29 13:16:09 24 4
gpt4 key购买 nike

我正在使用最新的 SDK 开发 iOS 应用程序。

我有一个带有自定义 UITableViewCellUITableView:

@interface FavouriteCell : UITableViewCell

@property (unsafe_unretained, nonatomic) IBOutlet UIImageView *selectIcon;
@property (unsafe_unretained, nonatomic) IBOutlet UILabel *favName;

@property (nonatomic) BOOL checked;

+ (NSString *)reuseIdentifier;

@end

selectIcon 上,我设置了正常和突出显示的两个图像。当我在 tableView:didSelectRowAtIndexPath: 上执行 cell.checked = !selected; 时,它工作得很好。

但是当我尝试重置 clearSelectedFavourites: 上的每个选定单元格时,它不起作用。

@interface FavViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate, UIAlertViewDelegate>
{
@private
NSMutableArray* _favsSelected;
BOOL* _isOnEditingMode;
}

// ######### FavViewController implementation ##############

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
self.title = NSLocalizedString(@"Favoritos", @"Favoritos");
self.tabBarItem.image = [UIImage imageNamed:@"fav"];

_favsSelected = [[NSMutableArray alloc] init];
_isOnEditingMode = NO;

}
return self;
}

- (UITableViewCell* )tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"cellForRowAtIndexPath");
static NSString* CellIdentifier = @"FavCell";

FavouriteCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:@"FavouriteCell" owner:self options:nil];
cell = _favCell;
self.favCell = nil;
}

cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.favName.font = [UIFont systemFontOfSize:15.0f];

[self configureCell:cell atIndexPath:indexPath];

return cell;
}

#pragma mark - UITableViewDelegate methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"didSelectRowAtIndexPath: %@", indexPath);

if (tableView.isEditing)
{
BOOL selected = NO;

if ((_favsSelected != nil) && ([_favsSelected count] > 0))
selected = ([_favsSelected indexOfObject:indexPath] != NSNotFound);

FavouriteCell* cell =
(FavouriteCell*)[tableView cellForRowAtIndexPath:indexPath];

NSLog(cell.checked ? @"Yes" : @"No");
cell.checked = !selected;

if (selected)
[_favsSelected removeObject:indexPath];
else
[_favsSelected addObject:indexPath];
}
}

- (IBAction)editFavList:(id)sender
{
NSLog(@"edit button clicked!");
if ([_favList isEditing])
{
[self clearSelectedFavourites];
[_favList setEditing:NO animated:YES];
[_editButton setImage:[UIImage imageNamed:@"ButtonEdit.png"] forState:UIControlStateNormal];
}
else
{
[_favList setEditing:YES animated:YES];
[_editButton setImage:[UIImage imageNamed:@"done_button.png"] forState:UIControlStateNormal];
}
}

- (void)clearSelectedFavourites
{
for(int index = 0; index < [_favsSelected count]; index++)
{
NSIndexPath* indexPath = (NSIndexPath*)[_favsSelected objectAtIndex:index];

FavouriteCell* cell = (FavouriteCell*)[self tableView:_favList cellForRowAtIndexPath:indexPath];
cell.checked = NO;
}

[_favsSelected removeAllObjects];
}

- (void)configureCell:(FavouriteCell *)cell
atIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *object = nil;

object = [self.favListFetchedResultsController objectAtIndexPath:indexPath];

cell.favName.text = [[object valueForKey:@"name"] description];
}

你知道为什么吗?

我已经尝试将 selectIcon 手动设置为不突出显示,但它不起作用,如果我进行了最新测试,则该代码。

我还在 clearSelectedFavourites: 上测试了这个:[cell setHighlighted:NO]; 但它不起作用。

最佳答案

您没有向我们展示您的自定义 [self configureCell:cell atIndexPath:indexPath]; 函数,所以我不知道发生了什么。

但是首先要注意几件事:

TableView 上的单元格正在被重用。因此每次都需要为每个单元格设置单元格选择状态。

同样在您的 clearSelectedFavourites 函数中,您将获得对所有选定单元格的引用,然后清除它们的选择,这是没有意义的,因为这些单元格将被重复使用。

您只需要遍历可见的 UITableView 单元格并更改它们的选择状态,然后清除您的 _favsSelected 数组...

- (void)clearSelectedFavourites
{

NSArray *cells = [self.tableView visibleCells];
for (FavouriteCell *cell in cells)
{
cell.checked = NO;
//might not be needed, or might be needed
[cell setNeedsDisplay];
}

[_favsSelected removeAllObjects];
}

最后,在您的 configureCell 函数中,您应该根据您的 _favsSelected 数组条目检查是否应该检查单元格,然后选择它或不选择它。

编辑

在您的 configureCell 函数中,您应该检查单元格是否需要检查:

- (void)configureCell:(FavouriteCell *)cell
atIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *object = nil;

object = [self.favListFetchedResultsController objectAtIndexPath:indexPath];

cell.favName.text = [[object valueForKey:@"name"] description];
cell.checked = [_favsSelected containsObject:indexPath];
}

关于ios - 将 `UIImageView` 设置为不在自定义 UITableViewCell 上突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15758978/

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