我正在使用最新的SDK开发iOS应用程序。
我有UITableView
个自定义UITableViewCell
:
@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
上我设置了两张正常和突出显示的图像。当我在cell.checked = !selected;
tableView:didSelectRowAtIndexPath:
时,它完美无缺。
但是当我尝试重置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
设置为手动不亮,但它不起作用,如果我做了最新的测试,该代码。
我还在[cell setHighlighted:NO];
上对此进行了测试:clearSelectedFavourites:
并且它不起作用。
答案 0 :(得分:1)
您没有向我们展示您的自定义[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];
}
答案 1 :(得分:0)
你可以这样做:
[tbl beginUpdates];
/ *用动画* /
调用你的方法[tbl endUpdates];
[tbl reloadData];