[英]Passing TableView Data to DetailViewController in swift
I have a simple tableview loading with array of colours(each cell with different colour).i am trying to pass the background colour of detailViewController when user press the colour array cell from colourTableviewcontroller(simply i want to pass the cell colour as a detailview background colour) my code as follows..... 我有一个简单的tableview加载颜色数组(每个单元格具有不同的颜色)。当用户从colourTableviewcontroller按下颜色数组单元格时,我试图传递detailViewController的背景色(只是我想将单元格颜色传递为detailview背景颜色)我的代码如下.....
import UIKit
class colourTableviewcontroller: UITableViewController {
@IBOutlet weak var colorTableView: UITableView!
let colors = [UIColor.redColor(), UIColor.blueColor(),UIColor.greenColor(), UIColor.orangeColor(),UIColor.purpleColor(),UIColor.yellowColor(),UIColor.cyanColor(),UIColor.darkGrayColor(),UIColor.blackColor(), UIColor.brownColor(),UIColor.grayColor(), UIColor.magentaColor(), UIColor.whiteColor()]
var colourData = UIColor()
override func viewDidLoad() {
super.viewDidLoad()
// 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.
}
override func viewDidAppear(animated: Bool) {
colorTableView.separatorColor = UIColor.clearColor()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return colors.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("colourCell", forIndexPath: indexPath) as UITableViewCell
cell.backgroundColor = self.colors[indexPath.row % self.colors.count]
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
colourData = self.colors[indexPath.row]
performSegueWithIdentifier("colourSegue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "colourSegue") {
let viewController = segue.destinationViewController as! DetailViewController
viewController.bc?.backgroundColor = colourData }
}}
// My DetailViewController as follows...
import UIKit
class DetailViewController: UIViewController {
@IBOutlet var bc: UIView!
override func viewDidLoad() {
self.bc.backgroundColor = UIView.appearance().backgroundColor
}
when i run the code my DetailViewController background colour changing to dark text colour when i press the colour array..... 当我运行代码时,当我按颜色数组时,DetailViewController背景颜色将变为深色文本颜色。
i don't know what i am missing or am i doing wrong approach/logic.... 我不知道我在想什么,或者我做错了方法/逻辑...。
Thanks in Advance..... 提前致谢.....
First of all in ColourTableviewcontroller
connect the segue to the table view cell rather than to the view controller and delete the method didSelectRowAtIndexPath
首先,在
ColourTableviewcontroller
中将segue连接到表视图单元而不是视图控制器,并删除方法didSelectRowAtIndexPath
Replace prepareForSegue
with 将
prepareForSegue
替换为
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "colourSegue" { let viewController = segue.destinationViewController as! DetailViewController let selectedIndexPath = tableView.indexPathForCell(sender as! UITableViewCell) viewController.color = colors[selectedIndexPath.row] } }
In DetailViewController
declare a variable color
在
DetailViewController
声明变量color
var color : UIColor!
And set the color of bc
in viewDidLoad()
并在
viewDidLoad()
设置bc
的颜色
bc.backgroundColor = color
The reason for the additional variable is that the IBOutlets
in DetailViewController
don't exist yet while prepareForSegue
is executed. 附加变量的原因是执行
prepareForSegue
DetailViewController
中的IBOutlets
还不存在。
You aren't passing the UIColor and assigning it in the next View Controller 您没有传递UIColor并在下一个View Controller中分配它
You just need to do few modifications to your existing code 您只需要对现有代码进行少量修改
1) Add var recievedColor : UIColor!
1)添加
var recievedColor : UIColor!
in the DetailViewController 在DetailViewController中
2) In your prepareForSegue replace viewController.bc?.backgroundColor = colourData
with viewController.recievedColor = colourData
2)在您的prepareForSegue中,将
viewController.bc?.backgroundColor = colourData
为 viewController.recievedColor = colourData
3) In your DetailViewController, in viewDidLoad() function, replace self.bc.backgroundColor = UIView.appearance().backgroundColor
with self.bc.backgroundColor = recievedColor
3)在你的DetailViewController,在viewDidLoad中()函数,取代
self.bc.backgroundColor = UIView.appearance().backgroundColor
与 self.bc.backgroundColor = recievedColor
And there you go! 然后你走了!
You need to pass the color, then assign the color to the view on the viewDidLoad()
. 您需要传递颜色,然后将颜色分配给
viewDidLoad()
上的视图。 You are assigning a variable to a view that hasn't been initialized yet. 您正在将变量分配给尚未初始化的视图。
in your detail class do as follow: 在您的详细信息类中,请执行以下操作:
class DetailViewController: UIViewController {
@IBOutlet var bc: UIView!
var selectedBackgroundColor: UIColor? // this should be set on the prepareForSegue method.
override func viewDidLoad() {
super.viewDidLoad()
if let selectedBackgroundColor = selectedBackgroundColor {
bc.backgroundColor = selectedBackgroundColor
}
}
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.