TableView 单元格点击到另一个 TableView

TableView Cell click to another TableView

我的应用程序上有一个 tableView。我想单击一个单元格并转到另一个单元格 tableView。例如艺术家列表,然后是专辑列表。

通常我只是将“Prototype Cells”拖到另一个视图控制器。但是没有任何反应。

这是我的代码:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return NumberOfArtists
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")

    print(NameOfArtist)

    cell.textLabel?.text = NameOfArtist[indexPath.row]
    cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator

    return cell

}

这是我的故事板:

更新:我已经从 TableView(背面的那个)中删除了 segue。还是不行。

步骤

1 - 创建一个 tableView 并添加源和委托。 2 - 创建另一个 View Controller 并添加一个 tableView。 3 - 创建从第一个原型单元格到第二个 tableView 的 segue。

没有任何反应。

2015 年 8 月 13 日编辑

  1. 您必须像您提到的那样为艺术家创建一个 UITableView。
  2. 你创建它你想要专辑艺术家的另一个 UITableView。
  3. 您只需 link 从原型单元格或所有静态单元格到 UITableView 的 segue,它应该可以工作。

注意:这是管理故事板的方法。我没有包括如何填充数据和管理 UITableView

我查看了您发布到 Dropbox 的代码。这是您的代码有什么问题:

  1. 您需要将主视图控制器嵌入到导航控制器中。您可以通过单击故事板中的主视图控制器,然后单击编辑器 -> 嵌入 -> 导航控制器选项来执行此操作。

  2. 您在 cellForRowAtIndexPath 方法中创建的单元格与您在故事板中附加转场的单元格不同。您可以像这样创建单元格:

     let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
    

这实际上创建了一个具有重用标识符 "Cell" 的全新单元格。它没有附加 segue。为此:

let cell : UITableViewCell! = tableView.dequeueReusableCellWithIdentifier("Cell");

这将在您的情节提要中获取单元格。 希望这有帮助。

我认为您不需要两个 viewController,根据您的设计,您可以为 ArtistAlbum 控制器使用第一个 tableView 和 Cell。

您只需要两个 DataSource 1 个用于艺术家,另一个用于 Album 和一个管理当前可见的布尔值 DataSource.than 使用 tableView 委托方法,您可以同时管理艺术家和专辑视图。 .

例如: 假设我们的布尔值 NameisArtistisArtist = true 对于 Artist ViewisArtist = false 对于 Album View.

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
     return isartist ? ArtistDataSource.Count : AlbumDataSource.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")

 if isArtist {
    cell.textLabel?.text = ArtistDataSource[indexPath.row]
 }
else {
    cell.textLabel?.text = AlbumDataSource[indexPath.row]
 }


    cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator

    return cell

}


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

          if isArtist {
            isartist.toggle()
            yourtableView.reloadData
          }
          else {
       //Do what you wish for Album tableView Selection
      }
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return isartist ? 70 : 100
    }