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

时间:2012-02-27 14:37:41

标签: objective-c ios xcode ipad ios5

- (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文件中声明 我创建了tableview,它根据单元格中的描述动画或不动画..当它动画时,如果我选择一行行大小增加,当我再次点击它时行大小应该回到正常高度,我当我选择另一行/单元格时,我才能做到这一点,当我点击所选行时,我希望单元格返回到正常高度。

1 个答案:

答案 0 :(得分:1)

此代码刚刚经过测试,可以正常运行。您必须根据需要对其进行修改,但基本行为应符合您的需求。

请注意,无需拨打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