行未在 UITableview 单元格动画中取消选择


- (BOOL)cellIsSelected:(NSIndexPath *)indexPath 
{
    // Return whether the cell at the specified index path is selected or not
    NSNumber *selectedIndex = [selectedIndexes objectForKey:indexPath];
    return selectedIndex == nil ? FALSE : [selectedIndex boolValue];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    // Deselect cell
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    // Toggle 'selected' state
    BOOL isSelected = ![self cellIsSelected:indexPath];
    // Store cell 'selected' state keyed on indexPath  
    NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected];
    [selectedIndexes setObject:selectedIndex forKey:indexPath];   
    // This is where magic happens...
    [beveragesTableView beginUpdates];
    [beveragesTableView endUpdates];
    [selectedIndexes removeAllObjects];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    // If our cell is selected, return double height
    if ([self cellIsSelected:indexPath] && ![[descriptionTypeArray objectAtIndex:indexPath.row] isEqualToString:@" "]) 
    {
        return kCellHeight * 2.0;
    }
    // Cell isn't selected so return single height
    return  kCellHeight;
}

selectedIndexes 是 NSMutableDictonary 的对象,在 .h 文件中声明我已经创建了表格视图,它根据单元格中是否存在的描述进行动画处理..虽然它动画,如果我选择一行,行大小会增加,当我再次点击它时,行大小应该回到正常高度,我只能在选择另一行/单元格时这样做,我想单元格在点击所选行时回到正常高度。

这段代码刚刚经过测试,它可以工作。您必须根据需要对其进行修改,但基本行为应适合您的需求。

请注意,无需调用beginUpdates/endUpdates

ViewController.h

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSIndexPath *lastSelectedCell;;
@end

ViewController.m

#import "ViewController.h"
@implementation ViewController
@synthesize tableView = _tableView;
@synthesize lastSelectedCell = _lastSelectedCell;;
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
    if (cell == nil)  {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyIdentifier"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = [NSString stringWithFormat:@"row %d %@",indexPath.row, ([indexPath compare:_lastSelectedCell] == NSOrderedSame)?@"S":@"-"];
    return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"S: %d", indexPath.row);
    if (([indexPath compare:_lastSelectedCell] == NSOrderedSame)) {
        _lastSelectedCell = nil;
    } else {
        [self setLastSelectedCell: indexPath];
    }
    [tableView reloadData];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return  ([indexPath compare:_lastSelectedCell] == NSOrderedSame)?80.0:40.0;
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
    [super viewDidLoad];
    _tableView.dataSource = self;
    _tableView.delegate = self;
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    _tableView = nil;
    _lastSelectedCell = nil;
}
@end

最新更新