【发布时间】:2015-08-03 14:04:26
【问题描述】:
我有一个两部分的表格视图(见下图)
当我点击“A”行时,另一个表格视图会显示字母“A”
当我单击“B”行时,会出现另一个带有字母“B”的表格视图
当我单击 C 行时,如何将字母“C”传递给另一个 tableview?
这是我在 TableView 中的代码: 导入 UIKit
类 manhinh1: UITableViewController {
var UserDefault:NSUserDefaults!
var array:[[String]]!
override func viewDidLoad() {
super.viewDidLoad()
UserDefault = NSUserDefaults()
array = [["A","B"],["C","D","E"]]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return array.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
if section == 0 {
return array[0].count
}
if section == 1 {
return array[1].count
}
return 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
if indexPath.section == 0 {
cell.textLabel?.text = array[0][indexPath.row]
}
if indexPath.section == 1 {
cell.textLabel?.text = array[1][indexPath.row]
}
// Configure the cell...
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var row = self.tableView.indexPathForSelectedRow()!.row
UserDefault.setObject(array[0][row], forKey: "selected")
}
}
这是第二个视图中的代码: 导入 UIKit
类视图控制器:UIViewController {
var UserDefault:NSUserDefaults!
@IBOutlet weak var lbldata: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
UserDefault = NSUserDefaults()
// Do any additional setup after loading the view, typically from a }
lbldata.text = UserDefault.objectForKey("selected") as! String
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我确实在 prepareForSegue 中尝试了一些代码,但没有成功。我希望你们能给我一个方法。谢谢
【问题讨论】:
标签: ios iphone uitableview