ios - 如何根据单击的单元格发送特定数据?

标签 ios swift

我有一个包含一堆音乐会的表格 View ,当单击 x 单元格时,我希望该音乐会的艺术家填充新的表格 View 。下面是第一个 View Controller (包含所有音乐会的 View Controller )的代码。

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {


@IBOutlet weak var tableView1: UITableView!
var arrayOfConcerts: [concert] = [concert]()


override func viewDidLoad()
{
    super.viewDidLoad()
    self.setUpConcerts()
    self.tableView1.rowHeight = 145.0
}

func setUpConcerts()
  {
    var ACL = concert(imageName: "ACL2015.png")
    let Landmark = concert(imageName: "Landmark.png")
    let BostonCalling = concert(imageName: "BostonCalling.png")
    let Lolla = concert(imageName: "Lolla.png")
    arrayOfConcerts.append(ACL)
    arrayOfConcerts.append(Landmark)
    arrayOfConcerts.append(BostonCalling)
    arrayOfConcerts.append(Lolla)

}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return arrayOfConcerts.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{
    let cell = self.tableView1.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell
    let concerts = arrayOfConcerts[indexPath.row]
    cell.setCell(concerts.imageName)

    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    performSegueWithIdentifier("concertartist", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
{

}

}

Below is the code for the Artist View Controller (the second viewcontroller). This tableView should be populated with specific artists.

我该怎么做呢?

class ArtistConcerts: UIViewController, UITableViewDataSource, UITableViewDelegate {

var arrayOfArtists: [artist] = [artist]()

@IBOutlet weak var tableViewArtists: UITableView!



override func viewDidLoad() 
{
    super.viewDidLoad()
    self.setUpArtists()
}

func setUpArtists()
{

}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return arrayOfArtists.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = self.tableViewArtists.dequeueReusableCellWithIdentifier("", forIndexPath: indexPath) as! CustomCell
    let artists = arrayOfArtists[indexPath.row]
    cell.setCell(artists.imageNameArtist)
    return cell
}

}

最佳答案

您需要在 prepareForSegue 方法中执行此操作。

 let vc  = segue!.destinationViewController as! ArtistConcerts

 if let rowSelected = tableView.indexPathForSelectedRow()?.row {
     vc.arrayOfArtists = artistsPerforming[0]//this is an array
 }

然后,设置在新 View Controller 中实际填充 tableView 所需的任何数据。不过,我不是 100% 确定您要做什么。我的回答有道理吗?

关于ios - 如何根据单击的单元格发送特定数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33069686/

相关文章:

java - 为什么同一个通知会多次推送到安卓和iphone设备

javascript - Ipad iOS 滚动不一致 : disable body scroll but allow scroll on certain elements

ios - 尚未购买的 IAP 正在恢复

ios - UIPickerView 作为多个 UITextField 的 inputView

ios - 如何创建以 TabBarViewController 为根的导航堆栈?

ios - 在 iOS7 中取消交互式弹出手势时如何将数据传递给子 Controller ?

ios - 我在界面生成器中创建了一个 UIImage View ,如何在代码中更改其框架(位置和大小)?

具有多种结构的 JSONEncoder

swift - 在 Swift 中将 char 添加到 int

iOS Swift 描边宽度