didSelectRowAtIndexPath和将Objective-C转换为Swift的实用方法?

时间:2015-03-15 22:58:29

标签: ios objective-c xcode swift

我正在尝试将此转换为swift但我被阻止了。我现在和将来如何实现这一目标?

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    if (self.tableView(tableView, canCollapseSection:indexPath.section)) {



            // only first row toggles exapand/collapse
            (tableView, deselectRowAtIndexPath:indexPath, animated:true);

            var section: NSInteger = indexPath.section;
            var currentlyExpanded: Bool = expandedSections.containsIndex(section);
            var rows: NSInteger;

            var tmpArray: NSMutableArray;

            if currentlyExpanded
            {
                rows = (self.tableView(tableView, numberOfRowsInSection:section))
                expandedSections.removeIndex(section)
            }
            else
            {
                expandedSections.addIndex(section)
                rows = (self.tableView(tableView, numberOfRowsInSection:section))
            }

            for var i = 1; i < rows; ++i
            {
                var tmpIndexPath: NSIndexPath = (indexPathForRow:i, inSection:section);
                tmpArray.addObject(tmpIndexPath);
            }

            var cell: UITableViewCell = (self.tableView, cellForRowAtIndexPath:indexPath);

            if currentlyExpanded
            {
                (tableView, deleteRowsAtIndexPaths:tmpArray, withRowAnimation:UITableViewRowAnimationFade);

            }
            else
            {
                (tableView, insertRowsAtIndexPaths:tmpArray, withRowAnimation:UITableViewRowAnimationFade);
            }
        }



}

我找不到for flow的正确代码,其中“var tmpIndexPath:NSIndexPath等于(indexPathForRow:i,inSection:section);”并且有行动画的部分似乎对我没有答案。 提前谢谢!

编辑:这是我想要转换的Objective-C代码。

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath (NSIndexPath *)indexPath {
     if ([self tableView:tableView canCollapseSection:indexPath.section])
    {
     if (!indexPath.row)
     {
        // only first row toggles exapand/collapse
        [tableView deselectRowAtIndexPath:indexPath animated:YES];

        NSInteger section = indexPath.section;
        BOOL currentlyExpanded = [expandedSections containsIndex:section];
        NSInteger rows;

        NSMutableArray *tmpArray = [NSMutableArray array];

        if (currentlyExpanded)
        {
            rows = [self tableView:tableView numberOfRowsInSection:section];
            [expandedSections removeIndex:section];
        }
        else
        {
            [expandedSections addIndex:section];
            rows = [self tableView:tableView numberOfRowsInSection:section];
        }

        for (int i=1; i<rows; i++)
        {
            NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
                                                           inSection:section];
            [tmpArray addObject:tmpIndexPath];
        }

        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

        if (currentlyExpanded)
        {
            [tableView deleteRowsAtIndexPaths:tmpArray
                             withRowAnimation:UITableViewRowAnimationFade];

            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        else
        {
            [tableView insertRowsAtIndexPaths:tmpArray
                             withRowAnimation:UITableViewRowAnimationFade];

            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

你在这里弄错了: 目的-C

NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
                                                           inSection:section];

成为SWIFT

var tmpIndexPath = NSIndexPath(forRow: i, inSection: section);

你多次犯了这种错误。