Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何知道是否选择了UITableView的单元格?_Ios_Xcode_Uitableview_Swift - Fatal编程技术网

Ios 如何知道是否选择了UITableView的单元格?

Ios 如何知道是否选择了UITableView的单元格?,ios,xcode,uitableview,swift,Ios,Xcode,Uitableview,Swift,我正在用XCode编写一个Swift应用程序。它包括一个UITableView。只有一个问题-在Swift中,我如何知道用户选择了哪个单元格 澄清: 用户选择标签为foo的单元格 代码应该返回foo或用户选择的任何内容 您可以通过在视图控制器中添加以下函数来判断用户选择的单元格 class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { ... func tableView(table

我正在用XCode编写一个Swift应用程序。它包括一个UITableView。只有一个问题-在Swift中,我如何知道用户选择了哪个单元格

澄清:

用户选择标签为foo的单元格 代码应该返回foo或用户选择的任何内容
您可以通过在视图控制器中添加以下函数来判断用户选择的单元格

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
...

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
    println("You selected cell #\(indexPath.row)!")
}

...
}

因此,现在取决于是否有一个数组来显示每个单元格的文本名称。如果是,则可以使用此indexath.row检索用户所选行的文本

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    var tableView : UITableView!
    var data:String[] = ["Cell 1","Cell 2","Cell 3","Cell 4"]


func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
        let cell : UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as UITableViewCell
        cell.text = self.data[indexPath.row]
        return cell
    }

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
         println("SELECTED INDEX /(indexPath.row)")
         println("Selected Cell Text /(data[indexPath.row])")
}

您可以通过在视图控制器中添加以下函数来判断用户选择的单元格

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
...

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
    println("You selected cell #\(indexPath.row)!")
}

...
}

因此,现在取决于是否有一个数组来显示每个单元格的文本名称。如果是,则可以使用此indexath.row检索用户所选行的文本

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    var tableView : UITableView!
    var data:String[] = ["Cell 1","Cell 2","Cell 3","Cell 4"]


func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
        let cell : UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as UITableViewCell
        cell.text = self.data[indexPath.row]
        return cell
    }

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
         println("SELECTED INDEX /(indexPath.row)")
         println("Selected Cell Text /(data[indexPath.row])")
}

假设您拥有相关单元格的indexPath-例如,在任何UITableViewDelegate或UITableViewDataSource方法中

BOOL selected = [tableView.indexPathsForSelectedRows containsObject:indexPath];

假设您拥有相关单元格的indexPath-例如,在任何UITableViewDelegate或UITableViewDataSource方法中

BOOL selected = [tableView.indexPathsForSelectedRows containsObject:indexPath];

否,您将函数添加到表视图委托。请再次检查我的UIViewController定义。它已经实现了委托,因此定义了函数,但不是每个人都这样做。该函数与委托一起使用,而不是与表视图一起使用。保持概念清晰是很重要的。我认为您应该检查一下apple示例:我并不是说您不能将委托和数据源与控制器结合起来。我是说,无论您如何组合它们,该函数在逻辑上都是委托的一部分。不,您将该函数添加到表视图委托中。请再次检查我的UIViewController定义。它已经实现了委托,因此定义了函数,但不是每个人都这样做。该函数与委托一起使用,而不是与表视图一起使用。保持概念清晰是很重要的。我认为您应该检查一下apple示例:我并不是说您不能将委托和数据源与控制器结合起来。我是说,无论你如何组合它们,该函数在逻辑上都是委托的一部分。你尝试过下面的方法吗?@bllakjack谢谢!我一有时间就会试试。@bllakjakk它能用。谢谢。你试过下面的方法吗?@bllakjakk谢谢!我一有时间就会试试。@bllakjakk它能用。谢谢