【问题标题】:didSelectRowAtIndexPath and a practical way to convert Objective-C to Swift?didSelectRowAtIndexPath 以及将 Objective-C 转换为 Swift 的实用方法?
【发布时间】:2015-03-15 22:58:29
【问题描述】:

我正在尝试将其转换为 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 流的正确代码,其中“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;
        }
    }
}

【问题讨论】:

    标签: ios objective-c xcode swift


    【解决方案1】:

    你在这里犯了一个错误: 目标-C

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

    加入 SWIFT

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

    你多次犯过这种错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-24
      • 1970-01-01
      • 1970-01-01
      • 2014-11-08
      相关资源
      最近更新 更多