尝试为单元格创建两个动画

时间:2015-07-27 05:31:16

标签: ios objective-c xcode6

我想通过展开和折叠来创建单元格。为此,我通过重新加载来为单元设置动画。当我扩展它工作正常。但当我崩溃它崩溃,原因是尝试为单元格创建两个动画。我知道我已经给出了相同的数组路径,并且它当时不会重新加载两个单元格。有没有办法解决这个问题。

我是编码的新手,所以我很乐意得到一个简单的解决方案。

代码:

使用lastSelIPath的原因是第一个单元格被展开时触摸第二个单元格,第一个单元格将崩溃,第二个单元格将展开。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSIndexPath* lastSelIPath = [ NSIndexPath indexPathForRow:selectedIndex_ inSection:0 ];

    [ tableView deselectRowAtIndexPath:indexPath animated:YES ];

    if (selectedIndex_ == indexPath.row)
    {
        selectedIndex_ = -1;
    }
    else
    {
        selectedIndex_ = indexPath.row;
    }

    NSIndexPath* ipath = [ NSIndexPath indexPathForRow:indexPath.row inSection:0 ];

    NSArray* indexPathArr = [ NSArray arrayWithObjects:lastSelIPath, ipath, nil ];


    [ tableView beginUpdates ];

    [ tableView reloadRowsAtIndexPaths:indexPathArr withRowAnimation:UITableViewRowAnimationAutomatic ];

    [ tableView endUpdates ];
}

所以我现在做了什么,我设定了条件并重新加载我想要的那个。这是一个正确的方法,请帮助。

修改后的代码:

if ( lastSelIPath.row == ipath.row )
    {
        [tableView reloadRowsAtIndexPaths:@[ipath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    else
    {
        [tableView reloadRowsAtIndexPaths:@[lastSelIPath] withRowAnimation:UITableViewRowAnimationAutomatic];

        [tableView reloadRowsAtIndexPaths:@[ipath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }

4 个答案:

答案 0 :(得分:2)

崩溃是对的,你正试图尝试两个动画。 indexPathArr包含两个对象。不要创建这个数组并尝试这个:

 [tableView reloadRowsAtIndexPaths:@[lastSelIPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView reloadRowsAtIndexPaths:@[ipath] withRowAnimation:UITableViewRowAnimationAutomatic];

如果有帮助,请告诉我

答案 1 :(得分:1)

[tableView reloadRowsAtIndexPaths:@[newIndexPath, oldIndexPath] withRowAnimation:UITableViewRowAnimationNone];

如果newIndexPath与oldIndexPath相同,它将在iOS8中崩溃,但这在iOS9及更高版本中已得到修复。

答案 2 :(得分:0)

{{1}}

使用选定的标签写入单元格扩展操作的条件。可能有帮助。

答案 3 :(得分:0)

您可以使用第三方库扩展单元格。 https://github.com/bennyguitar/CollapseClick  &安培;将委托方法实现为

-(int)numberOfCellsForCollapseClick
{
}
-(NSString *)titleForCollapseClickAtIndex:(int)index {
}
-(UIView *)viewForCollapseClickContentViewAtIndex:(int)index {
}