【问题标题】:TableViewCell in Swift?Swift 中的 UITableViewCell?
【发布时间】:2014-09-15 20:40:36
【问题描述】:

我在 Objective C 中有这段代码,想知道你是否可以将它翻译成新的 swift Xcode... 这是代码。

  (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = cell.textLabel.text;
 }

【问题讨论】:

  • 我拒绝相信 stackoverflow 是免费的翻译服务。

标签: xcode swift


【解决方案1】:

这应该是正确的:

func tableView(tableView: UITableView, indexPath: NSIndexPath) {
   let cell = tableView.cellForRowAtIndexPath(indexPath);
   let cellText = cell.textLabel.text;
 }

【讨论】:

  • 这应该会产生错误“使用未声明的类型:'tableView'”
  • 这不是语法有效的 Swift。 tableView 参数从不指定类型,而 indexPath 参数的参数类型和名称颠倒了。这仍然不是有问题的 UITableViewDelegate 方法,但它应该更像tableView: UITableView, indexPath: NSIndexPath
  • 修复了!! :) 现在参考方法 didselectrowatindexpath 中的代码我怎样才能得到所选单元格中的字符串??
  • 前往 Apple 的 swift 书籍并阅读有关可选项的内容——因为 Colin 也弄错了。
猜你喜欢
  • 2014-08-17
  • 1970-01-01
  • 2018-03-25
  • 1970-01-01
  • 2020-02-17
  • 2015-08-16
  • 1970-01-01
  • 2015-09-01
  • 2017-09-18
相关资源
最近更新 更多