[英]Issue with selecting a row in a tableView in swift
通过查看标题,您几乎可以意识到我的问题是,我在情节提要上有一个表格视图,无论我尝试执行以下这段代码时,无论尝试执行什么操作,仍然没有任何反应:
func tableView (tableView:UITableView , cellForRowAtIndexPath indexPath : NSIndexPath)->UITableViewCell{
var cellIdentifier = "Cell"
var cell :UITableViewCell
cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as UITableViewCell
var st = indexPath
if (cell == nil){
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellIdentifier)
}
var word:NSString
word=message[indexPath.row] as NSString
if word.containsString("You just sent a message"){
cell.textLabel.text = word.componentsSeparatedByString("\n")[1].componentsSeparatedByString(":")[6] as NSString}
if word.containsString("Users"){
var smth = word.componentsSeparatedByString(":")
var word = ""
for i in (1..<smth.count){
var hey = smth[i] as NSString
var elst = ""
elst += hey as NSString
word += elst + " "
}
cell.textLabel.text = word
}
else
{
cell.textLabel.text = word
}
return cell
}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
println("You selected cell #\(indexPath.row)!")
}
func tableView(tableView:UITableView ,numberOfRowsInSection section :NSInteger)->Int{
return message.count
}
经过一番调查之后,我发现didSelectRowAtIndexPath函数是负责的函数,但是在实现它之后仍然没有任何反应
如您所见,主函数执行某些操作,并且确实在每个单元格中添加了一些东西[正确的事情]
任何想法为什么会发生这种情况?
您是否将表格视图的委托设置为该swift类? 表格视图在IB中在哪里,还是您使用代码创建的? 如果在代码中必须手动设置委托,则在IB中,则可以拖放以设置委托。
如果设置了表格视图并通过代码对其进行了初始化,
import UIKit
class ViewController: UIViewController,UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
var tableView:UITableView = UITableView()
tableView.delegate = self;
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
注意:没有在UIViewController旁边指定上面的UITableViewDelegate,我得到了一个错误,但是同样会在Objective C中产生警告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.