objective-c - 从 Swift 到 Objective C - updateCallDurationForVisibleCells

标签 objective-c swift

我正在尝试用 Objective-C 隐藏 Speakrbox 代码。 我已经转换了大部分代码,但我对此有一点问题:

private func updateCallDurationForVisibleCells() {
    /*
        Modify all the visible cells directly, since -[UITableView reloadData] resets a lot
        of things on the table view like selection & editing states
     */
    let visibleCells = tableView.visibleCells as! [CallSummaryTableViewCell]

    guard let indexPathsForVisibleRows = tableView.indexPathsForVisibleRows else { return }

    for index in 0..<visibleCells.count {
        let cell = visibleCells[index]
        let indexPath = indexPathsForVisibleRows[index]

        guard let call = call(at: indexPath) else { return }
        cell.durationLabel?.text = durationLabelText(forCall: call)
    }
}

我尝试对其进行转换,这就是我所拥有的:

-(void) updateCallDurationForVisibleCells :(id)sender {
    /*
     Modify all the visible cells directly, since -[UITableView reloadData] resets a lot
     of things on the table view like selection & editing states
     */

    _visibleCells = _tableview.visibleCells;

    if(_indexPathsForVisibleRows == _tableview.indexPathsForVisibleRows) { return ; }
    int index;
    for (index=0; index<_visibleCells.count; index ++)
    {
        UICollectionViewCell *cell = _visibleCells[index];
        NSIndexPath *indexPath = _indexPathsForVisibleRows[index];
        UITableViewCell* call;

        if((call = [self.tableView cellForRowAtIndexPath:indexPath]))
          { return ; }

    }
}

有人可以帮我把这个 Swift 代码转换成 Objective-C 吗?该代码无法编译,因为我不知道如何转换这行代码:

cell.durationLabel?.text = durationLabelText(forCall: call) 

另外,我也不知道我的做法是否正确,尤其是guard let的转换。

您将在这里找到我在 pdateCallDurationForVisibleCells 函数中使用的 Call 和urationLabelText 函数:

  private func call(at indexPath: IndexPath) -> SpeakerboxCall? {
    return callManager?.calls[indexPath.row]
}//swift


private func durationLabelText(forCall call: SpeakerboxCall) -> String? {
    return call.hasConnected ? callDurationFormatter.format(timeInterval: call.duration) : nil
}//Swift

最佳答案

这是您的 Objective-C 代码的修正版本。请注意,当您在 Swift 中看到 varlet 时,您正在创建一个局部变量。当您应该只使用局部变量时,不要在 Objective-C 代码中创建一堆不必要的实例变量或属性。

- (void)updateCallDurationForVisibleCells {
    /*
     Modify all the visible cells directly, since -[UITableView reloadData] resets a lot
     of things on the table view like selection & editing states
     */

    NSArray *indexPathsForVisibleRows = self.tableView.indexPathsForVisibleRows;
    if(!indexPathsForVisibleRows) { return ; }

    NSArray *visibleCells = self.tableview.visibleCells;

    for (NSInteger index = 0; index < _visibleCells.count; index++) {
        CallSummaryTableViewCell *cell = visibleCells[index];
        NSIndexPath *indexPath = indexPathsForVisibleRows[index];

        SomeDataType *call = [self call:indexPath];
        if (!call) { return; }

        cell.durationLabel.text = [self durationLabelText:call];
    }
}

缺少一些信息,这意味着此转换可能不是 100% 正确。

  1. 您尚未显示 call: 方法的 Objective-C 签名,因此我无法确定如何正确调用它,也不知道该使用什么数据类型调用变量。将 SomeDataType 替换为正确的类型。
  2. 您尚未显示 durationLabelText: 方法的 Objective-C 签名,因此我无法 100% 确定调用它的正确方法。

如果您在问题中发布这些详细信息,我可以确定此答案已正确更新。

关于objective-c - 从 Swift 到 Objective C - updateCallDurationForVisibleCells,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43612055/

相关文章:

iphone - 在运行时创建新的对象模板 iPhone

ios - UITableview在每行出现之前无法平滑滚动?

ios - 如何使用适用于 iOS 的 swift 3 连接到 MEAN stack REST api

ios - “CGPointMake”在 swift 中不可用

ios - UISplitScreenController 具有非常多(超过 20 个)详细 View

iOS - 使用 PresentViewController 呈现 ViewController : doesn't work

iphone - 在添加到 View 之前在后台线程中更新 UI 元素

objective-c - 解析 .plist 项目

ios - 模拟位置 bt 在第二次运行时可用

Swift 2.2 单播放/暂停 UIButton