在UITableview单元格动画中未取消选择行 [英] row is not getting deselected in UITableview cell animation

查看:57
本文介绍了在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是.h文件中声明的NSMutableDictonary对象. 我已经创建了tableview,它根据是否存在单元格中的描述进行动画处理..在进行动画处理时,如果我选择一行,则行大小会增加,当我再次点击它时,行大小应回到正常高度,我我只能在选择另一行/单元格时执行此操作,因此我想在点击所选行时将单元格恢复到正常高度.

selectedIndexes is an object ofNSMutableDictonary which is declared in .h file i have created tableview where it animates based upon description present in the cell or not .. while it animates , if i select a row the row size increases and when i tap on it once again the row size should go back to normal height , i am only able to do that when i select another row/cell , i want to cell to go back to normal height when i tap on the selected row .

推荐答案

该代码已经过测试,可以正常工作.您将不得不根据需要对其进行修改,但是基本行为应适合您的需求.

This code was just tested and it works. You will have to modify it for your needs but the basic behaviour should suit your needs.

请注意,无需致电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

这篇关于在UITableview单元格动画中未取消选择行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆