我正在尝试用 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 中看到 var
或 let
时,您正在创建一个局部变量。当您应该只使用局部变量时,不要在 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% 正确。
- 您尚未显示
call:
方法的 Objective-C 签名,因此我无法确定如何正确调用它,也不知道该使用什么数据类型调用
变量。将SomeDataType
替换为正确的类型。 - 您尚未显示
durationLabelText:
方法的 Objective-C 签名,因此我无法 100% 确定调用它的正确方法。
如果您在问题中发布这些详细信息,我可以确定此答案已正确更新。
关于objective-c - 从 Swift 到 Objective C - updateCallDurationForVisibleCells,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43612055/