使用平滑动画展开UITableViewCell

时间:2014-09-04 20:28:37

标签: ios uitableview uiviewanimation

我正在尝试在UITableViewCell的高度发生变化时为其转换动画。为此,我使用以下代码行:

    [meetingsTable beginUpdates];
    [meetingsTable reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:changedRow inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
    [meetingsTable endUpdates];

但它没有显示行高度更改动画。它只显示扩展的单元格。但是,如果我删除该行

[meetingsTable reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:changedRow inSection:0]] withRowAnimation:UITableViewRowAnimationNone];

它显示预期的动画高度变化,但不重新加载单元格。我已经尝试了所有可用的UITableViewRowAnimation选项但没有成功。我也试过

[meetingsTable reloadData];

但它也没有帮助。 请建议可能的解决方案。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
BMeetingsTableCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:@"MEETINGS"];
if (!cell) {
    cell = [[BMeetingsTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MEETINGS"];
}

[self customizeCell:cell atIndexPath:indexPath forTable:tableView];
return cell;
}

- (void)customizeCell:(BMeetingsTableCell *)cell atIndexPath:(NSIndexPath *)indexPath forTable:(UITableView*)tableView shouldReload:(BOOL)shouldReload{
NSDictionary *event;
if (isSearching) {
    event = [NSDictionary dictionaryWithDictionary:[searchedData objectAtIndex:indexPath.row]];
}else if ([tableView isEqual:_activeMeetingsTable])
    event = [NSDictionary dictionaryWithDictionary:[activeEvents objectAtIndex:indexPath.row]];
else if ([tableView isEqual:_completedMeetingTable])
    event = [NSDictionary dictionaryWithDictionary:[completedEvents objectAtIndex:indexPath.row]];
NSArray *partArr;
UILabel *showTimeLbl = (UILabel *)[cell viewWithTag:9];
UIImageView *showNewMeetingIcon = (UIImageView *)[cell viewWithTag:8];
[showTimeLbl setHidden:YES];
[showNewMeetingIcon setHidden:YES];
cell.delegate = self;
cell.tag = indexPath.row;
NSMutableArray* foundConts = [[NSMutableArray alloc]init];
NSMutableArray *tempPartArr = [[NSMutableArray alloc]init];
for (NSDictionary *dict in [event valueForKey:@"users"]) {
    [tempPartArr addObject:dict];
}
partArr = [NSArray arrayWithArray:tempPartArr];
if ([displayName length]==0) displayName = numberString;
NSDictionary *participant = [[NSDictionary alloc]initWithObjectsAndKeys:inviteStat, @"inviteStatus", displayName, @"displayName", nil];
        [foundConts addObject:participant];
    }
}
if ([allParticipants count]> indexPath.row) {
    [allParticipants replaceObjectAtIndex:indexPath.row withObject:invitedConts];
}else
    [allParticipants addObject:invitedConts];
if ([tableView isEqual:_meetingsTable]  && selectedRow == indexPath.row) {
    cell.participants = [NSArray arrayWithArray:foundConts];
    [cell.contsTable setHidden:NO];
    //        [cell.arrowButton setSelected:YES];
    [cell.userImage setImage:[UIImage imageNamed:@"expandedProfilePic.png"]];
}else{
    [cell.userImage setImage:[UIImage imageNamed:@"ProfileImage.png"]];
    [cell.contsTable setHidden:YES];
    //        [cell.arrowButton setSelected:NO];
}


[cell.tapProfileBtn addTarget:self action:@selector(expandDetails:) forControlEvents:UIControlEventTouchUpInside];
cell.tapProfileBtn.tag = indexPath.row;
[cell.title setText:[event valueForKey:@"subject"]];

NSString* location;
if ([[event valueForKey:@"address"] isKindOfClass:[NSNull class]]) {
    location = @"";
}else
    location = [event valueForKey:@"location"];

long long startDateMilliSec = [[event valueForKey:@"start_time_milliseconds"] longLongValue];
NSDate *capturedStartDate = [NSDate dateWithTimeIntervalSince1970:startDateMilliSec];
//
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];
NSDateComponents *comps1 = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit  fromDate:capturedStartDate];
NSString *theYear = [NSString stringWithFormat:@", %ld", (long)comps1.year];
[dateFormatter setDoesRelativeDateFormatting:YES];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSString *dateString = [dateFormatter stringFromDate:capturedStartDate];
if ([dateString isEqualToString:@"Today"]||[dateString isEqualToString:@"Tomorrow"]) {
}else{
    [dateFormatter setDateFormat:@"dd MMMM yyyy"];
    [dateFormatter setDoesRelativeDateFormatting:NO];
    dateString = [dateFormatter stringFromDate:capturedStartDate];
}
NSString *amPM = (comps1.hour>=12)?@"PM":@"AM";
NSString *hourStr = [NSString stringWithFormat:@"%02d",(comps1.hour%12)];
dateString = [dateString stringByReplacingOccurrencesOfString:theYear withString:@""];
NSString *timeString = [NSString stringWithFormat:@"%@:%02d %@",hourStr, comps1.minute, amPM];
NSString *displayString = [NSString stringWithFormat:@"%@ | %@",dateString, timeString];
[cell.date setText:[NSString stringWithFormat:@"%@ | %@", displayString, location]];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == selectedRow) {
        NSDictionary* event = [activeEvents objectAtIndex:indexPath.row];
        NSArray *contacts = [event valueForKey:@"users"];
        int height = ([contacts count]*50)+70;
        return height;
    }
  else return 50
}

- (IBAction)expandDetails:(id)sender{
UIButton *btn = (UIButton*)sender;
selectedRow = btn.tag;
BMeetingsTableCell *changedCell = (BMeetingsTableCell*)[_activeMeetingsTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:btn.tag inSection:0]];
    [self customizeCell:changedCell atIndexPath:[NSIndexPath indexPathForRow:btn.tag inSection:0] forTable:_meetingsTable];
    [_meetingsTable beginUpdates];
    [_meetingsTable endUpdates];
}

2 个答案:

答案 0 :(得分:1)

您必须更新单元格而不重新加载表格视图。

我想你在cellForRowAtIndexPath中有你的单元格自定义代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //dequeue cell
    //customize cell at index path
    return cell;
}

将自定义移动到自带的单元格方法中。

- (void)customizeCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
   //customize cell
}

现在,当您需要更新单元格时,您可以执行此操作...

- (void)rowWasChanged:(NSInteger)changedRow {
    NSIndexPath *changedIndex = [NSIndexPath indexPathForRow:changedRow inSection:0];
    UITableViewCell *changedCell = [self.meetingsTable cellForRowAtIndexPath:changedIndex];
    [self customizeCell:changedCell atIndexPath:changedIndex];
    [self.meetingsTable beginUpdates];
    [self.meetingsTable endUpdates];
}

答案 1 :(得分:1)

//单元格动画

if(sender.tag == 0) {
    if(sender.on){
        rowHeight1 = 250;
    }else{
        rowHeight1 = 55;
    }
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
    NSArray* rowsToReload = [NSArray arrayWithObjects:indexPath, nil];
    [tblHealthQuestion reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationAutomatic];

}

//用于剖面动画

  NSRange range = NSMakeRange(senctionIndex, 1);//NSMakeRange(0, 1);
        NSIndexSet *section = [NSIndexSet indexSetWithIndexesInRange:range];
        [tblHealthQuestion reloadSections:section withRowAnimation:UITableViewRowAnimationAutomatic];

- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation{
    [self reloadSections:sections withRowAnimation:animation];
}