Ios 在自定义UITableViewCell上将“UIImageView”设置为不高亮显示
我正在用最新的SDK开发一个iOS应用程序 我有一个带有自定义Ios 在自定义UITableViewCell上将“UIImageView”设置为不高亮显示,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在用最新的SDK开发一个iOS应用程序 我有一个带有自定义UITableView单元格的UITableView: @interface FavouriteCell : UITableViewCell @property (unsafe_unretained, nonatomic) IBOutlet UIImageView *selectIcon; @property (unsafe_unretained, nonatomic) IBOutlet UILabel *favName; @pr
UITableView单元格的UITableView
:
@interface FavouriteCell : UITableViewCell
@property (unsafe_unretained, nonatomic) IBOutlet UIImageView *selectIcon;
@property (unsafe_unretained, nonatomic) IBOutlet UILabel *favName;
@property (nonatomic) BOOL checked;
+ (NSString *)reuseIdentifier;
@end
在选择icon
时,我设置了两幅正常和高亮显示的图像。当我做cell.checked=!选定的代码>在tableView:didSelectRowAtIndexPath:
上它工作得很好
但是,当我试图重置ClearSelectedFavorites:
上的每个选定单元格时,它不起作用
@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];
}
@interface FavViewController:UIViewController
{
@私人的
NSMUTABLEARRY*\u favsSelected;
BOOL*_isOnEditingMode;
}
//FavViewController实现##############
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我)
{
self.title=NSLocalizedString(@“Favoritos”,@“Favoritos”);
self.tabBarItem.image=[UIImage ImageName:@“fav”];
_favsSelected=[[NSMutableArray alloc]init];
_isOnEditingMode=否;
}
回归自我;
}
-(UITableView单元格*)表格视图:(UITableView*)表格视图
cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
NSLog(@“cellforrowatinexpath”);
静态NSString*CellIdentifier=@“FavCell”;
FavoriteCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil)
{
[[NSBundle mainBundle]loadNibNamed:@“FavoriteCell”所有者:自选项:nil];
细胞=_favCell;
self.favCell=nil;
}
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.favName.font=[UIFont systemFontOfSize:15.0f];
[self-configureCell:cell-atIndexPath:indexPath];
返回单元;
}
#pragma标记-UITableViewDelegate方法
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath
{
NSLog(@“didSelectRowAtIndexPath:%@”,indexath);
if(tableView.i编辑)
{
BOOL selected=否;
如果((_favsSelected!=nil)&([u favsSelected count]>0))
selected=([\u favsSelected indexOfObject:indexath]!=NSNotFound);
收藏夹单元格*单元格=
(FavoriteCell*)[tableView cellForRowAtIndexPath:indexPath];
NSLog(cell.checked?@“是”:@“否”);
cell.checked=!选中;
如果(选定)
[\u favsSelected removeObject:indexPath];
其他的
[\u favsSelected addObject:indexPath];
}
}
-(iAction)编辑列表:(id)发件人
{
NSLog(@“单击编辑按钮!”);
如果([[u favList isEditing])
{
[自选收藏夹];
[_favlistsetediting:否动画:是];
[\u editButton setImage:[UIImage ImageName:@“ButtonEdit.png”]用于状态:UIControlStateNormal];
}
其他的
{
[\u favList setEditing:YES动画:YES];
[\u editButton setImage:[UIImage ImageName:@“done\u button.png”]用于状态:UIControlStateNormal];
}
}
-(无效)清除选定的收藏夹
{
对于(int index=0;index<[u favsSelected count];index++)
{
nsindepath*indepath=(nsindepath*)[\u favsSelected objectAtIndex:index];
FavoriteCell*单元格=(FavoriteCell*)[self tableView:_FavlistCellforRowatineXpath:indexPath];
cell.checked=否;
}
[_favsselectedremoveallobjects];
}
-(void)配置单元格:(FavoriteCell*)单元格
atIndexPath:(nsindepath*)indepath
{
NSManagedObject*对象=nil;
object=[self.favListFetchedResultsController对象索引路径:indexPath];
cell.favName.text=[[object valueForKey:@“name”]description];
}
你知道为什么吗?
我曾尝试手动将selectIcon
设置为不高亮,但它不起作用,如果我最近做的测试显示该代码无效
我也测试过这个:[cell setHighlighted:NO]上的code>清除所选收藏夹:
但它不起作用。您可以这样做:
@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];
}
[tbl beginUpdates];
/*用动画调用你的方法*/
[tbl endUpdates];
[tbl reloadData];
您可以这样做:
[tbl beginUpdates];
/*用动画调用你的方法*/
[tbl endUpdates];
[tbl reloadData];
您没有向我们显示您的自定义[self-configureCell:cell-atIndexPath:indexath]代码>函数,所以我不知道发生了什么
然而,首先有几件事:
正在重用TableView上的单元格。
因此,每次需要为每个单元格设置单元格选择状态
另外,在clearselectedfavorites
函数中,您将获得对所有选定单元格的引用,然后清除它们的选择,这是毫无意义的,因为这些单元格将被重用
您只需在可见的UITableView
单元格中循环并更改其选择状态,然后清除您的\u 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
函数中,您应该根据您的\u 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];
}
您没有向我们显示您的自定义[self-configureCell:cell-atIndexPath:indexath]代码>函数,所以我不知道发生了什么
然而,首先有几件事:
正在重用TableView上的单元格。
因此,每次需要为每个单元格设置单元格选择状态
另外,在clearselectedfavorites
函数中,您将获得对所有选定单元格的引用,然后清除它们的选择,这是毫无意义的,因为这些单元格将被重用
您只需在可见的UITableView
单元格中循环并更改其选择状态,然后清除您的\u 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
函数中,您应该根据您的\u favsSelected
数组条目检查是否应该检查单元格,然后只选择它或不选择它
编辑
在您的