如何基于描述之类的东西为一个UITableView单元格/行设置动画?

时间:2012-02-26 16:03:17

标签: iphone objective-c xcode ipad ios5

我有一个单元格中的描述,标题..一些标题有一些没有的描述。我想根据描述动画(增加大小)行,如果没有描述,则单元格不应该增加大小,就好像存在单元格应该增加的描述一样。标题和描述的数据是从sql db中获取的.i已经创建了动画部分,我在增加单元格的大小时只有描述部分有麻烦.. - (void)tableViewUITableView *)tableView cellForRowIndexNSIndexPath *)indexPath我定义了descriptionlabel从descriptionArray获取值

- (BOOL)cellIsSelected:(NSIndexPath *)indexPath;

@end

@implementation LOAnimatedCellDemoViewController

#define kCellHeight 50.0

- (void)viewDidLoad {
    [super viewDidLoad];

    selectedIndexes = [[NSMutableDictionary alloc] init];
}

- (void)viewDidUnload {
    [selectedIndexes release];
    selectedIndexes = nil;

    [super viewDidUnload];
}

- (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];
}



#pragma mark -
#pragma mark Tableview Datasource Methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 20;
}

#pragma mark -
#pragma mark Tableview Delegate Methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Deselect cell
    [tableView deselectRowAtIndexPath:indexPath animated:TRUE];

    // 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...
    [demoTableView beginUpdates];
    [demoTableView endUpdates];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    // If our cell is selected, return double height
    if([self cellIsSelected:indexPath]) {
        return kCellHeight * 2.0;
    }

    // Cell isn't selected so return single height
    return kCellHeight;
}

@end

2 个答案:

答案 0 :(得分:1)

尝试将didSelectRowAtIndexPath替换为:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Deselect cell
    [tableView deselectRowAtIndexPath:indexPath animated:TRUE];

    // 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...
    [demoTableView beginUpdates];

    //these two lines were added
    NSArray *array = [NSArray arrayWithObject:indexPath]; 
    [self.tableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationNone];   

    [demoTableView endUpdates];
}

并确保在此过程中调用cellForRowAtIndexPath:时返回正确的(大/小)视图。

答案 1 :(得分:0)

in if([self cellIsSelected:indexPath])我添加的行

if([self cellIsSelected:indexPath] && ![descriptionArray objectAtIndex:indexpath.row] isEqualToString:@" "])  我给了 isEqualToString:@" " 导致数据库列中没有描述的项目我给了 单个空格字符 ,这样就表明没有描述是可用于该项目 -