Uitableview 如何在swift中获取所选行的textLabel?
因此,我尝试获取所选行的textLabel的值。我试着把它打印出来,但没用。经过一些研究,我发现这段代码有效,但只适用于Objective-CUitableview 如何在swift中获取所选行的textLabel?,uitableview,swift,ios8,tableview,xcode6,Uitableview,Swift,Ios8,Tableview,Xcode6,因此,我尝试获取所选行的textLabel的值。我试着把它打印出来,但没用。经过一些研究,我发现这段代码有效,但只适用于Objective-C - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"did select and the text is %@",[tableView cellForRowAtIn
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"did select and the text is %@",[tableView cellForRowAtIndexPath:indexPath].textLabel.text);]
}
我无法为斯威夫特找到任何解决方案。虽然可以打印indexpath.row,但这不是我需要的
那我该怎么办?或者该代码的“Swift版本”是什么?试试这个:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let indexPath = tableView.indexPathForSelectedRow() //optional, to get from any UIButton for example
let currentCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell
print(currentCell.textLabel!.text)
如果您在继承自
UITableViewController
的类中,则这是swift版本:
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
let cell = self.tableView.cellForRowAtIndexPath(indexPath)
NSLog("did select and the text is \(cell?.textLabel?.text)")
}
请注意,单元格
是可选的,因此必须将其展开-文本标签
也是如此。如果2中的任何一个为nil(不太可能发生,因为该方法是使用有效的索引路径调用的),如果要确保打印了有效的值,则应检查单元格和文本标签都不是nil:
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
let cell = self.tableView.cellForRowAtIndexPath(indexPath)
let text = cell?.textLabel?.text
if let text = text {
NSLog("did select and the text is \(text)")
}
}
维护一个数组,该数组将数据存储在cellforindexPath
方法本身中:-
[arryname objectAtIndex:indexPath.row];
在didselectaAtIndexPath
方法中也使用相同的代码。。祝你好运:)这将起作用:
let item = tableView.cellForRowAtIndexPath(indexPath)!.textLabel!.text!
如果要根据匹配的nsindepath
打印UITableViewCell
的文本,您必须使用UITableViewDelegate
的tableView:didselectRowatineXpath:
方法,并使用UITableView
的cellforRowatineXpath:
方法获取所选UITableViewCell
的引用
例如:
import UIKit
class TableViewController: UITableViewController {
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
switch indexPath.row {
case 0: cell.textLabel?.text = "Bike"
case 1: cell.textLabel?.text = "Car"
case 2: cell.textLabel?.text = "Ball"
default: cell.textLabel?.text = "Boat"
}
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let selectedCell = tableView.cellForRowAtIndexPath(indexPath)
print(selectedCell?.textLabel?.text)
// this will print Optional("Bike") if indexPath.row == 0
}
}
但是,出于许多原因,我不鼓励您使用前面的代码。您的UITableViewCell
应该只负责显示模型提供的某些内容在大多数情况下,您需要的是根据nsindepath
打印模型的内容(可以是字符串的数组
)。通过这样做,您将分离每个元素的责任
因此,我建议:
import UIKit
class TableViewController: UITableViewController {
let toysArray = ["Bike", "Car", "Ball", "Boat"]
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return toysArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
cell.textLabel?.text = toysArray[indexPath.row]
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let toy = toysArray[indexPath.row]
print(toy)
// this will print "Bike" if indexPath.row == 0
}
}
如您所见,使用此代码,您不必处理选项,甚至不需要在tableView:didSelectRowAtIndexPath:
中获取匹配的UITableViewCell
的引用,就可以打印所需的文本。在我的例子中,我在tableView select中搜索值时做了一些小的更改(didSelectRowAtIndexPath
)单元格返回单元格的索引,因此在将一个ViewController移动到另一个ViewController时遇到问题。通过使用此方法,我找到了重定向到新ViewController的解决方案
let indexPath = tableView.indexPathForSelectedRow!
let currentCellValue = tableView.cellForRow(at: indexPath!)! as UITableViewCell
let textLabelText = currentCellValue.textLabel!.text
print(textLabelText)
Swift 4
要获取所选行的标签,请执行以下操作:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! TableViewCell
print(cell.textLabel?.text)
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! TableViewCell
print(cell.textLabel?.text)
}
要获取取消选中的行的标签,请执行以下操作:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! TableViewCell
print(cell.textLabel?.text)
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! TableViewCell
print(cell.textLabel?.text)
}
在swift 4中:
通过重写方法
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storyboard = UIStoryboard(name : "Main", bundle: nil)
let next vc = storyboard.instantiateViewController(withIdentifier: "nextvcIdentifier") as! NextViewController
self.navigationController?.pushViewController(prayerVC, animated: true)
}
您好,我没有使用TableViewController,只是在ViewController中使用tableview。这对我来说一直都很好。当我将代码粘贴到ViewController类中时,它会出现一个错误,表示方法不会覆盖其超类中的任何方法。
。这可以通过删除覆盖
-部分来修复。当我尝试d再次运行代码,它显示(UITableView,numberofrowsinssection:Int)->int'没有名为'CellForRowatineXpath'的成员。
。我能做些什么来解决这个问题?您可能必须让视图控制器实现UITableViewDelegate
协议,并将表视图的delegate
属性设置为self-viewDidLoad
是一个好地方这对我来说很有效!我唯一需要做的就是要更改的ad是在indepath
和UITableViewCell
之后放置一个!
。非常感谢!从参数到indepath和通过tableView.indexPathForSelectedRow()获取之间有什么不同
?这是一样的。它也应该向您显示一个获取当前选定indexPath的方法。可以从视图中的任何按钮获取。Swift 4.1让indexPath=tableView.indexPathForSelectedRow让cell=tableView.cellForRow(位于:indexPath!)虽然此代码片段可能会解决问题,但确实有助于提高您的文章质量。请记住,您将在将来为读者回答此问题,而这些人可能不知道您的代码建议的原因。