Chr*_*ley 7 delegates protocols uitableview ios swift
我已经想出了在其他情况下如何在代表之间传递数据,但这个问题让我很难过.
在这个例子中,我试图通过按下按钮发送数据,使用委托模式直到标签,但没有任何成功.我的猜测是我错过了一些基本的东西,我没有找到任何以这种方式处理代表的例子.
//
// ViewController.swift
// TableCellDelegate
//
// Created by Chris Cantley on 6/1/15.
// Copyright (c) 2015 Chris Cantley. All rights reserved.
//
import UIKit
class ViewController: UIViewController, CellInfoDelegate {
var cellViewController = CellViewController()
//The place to put the number into.
@IBOutlet weak var sumLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
cellViewController.delegate = self
}
//2)...to here.
func processThatNumber(theNumber: Int) {
println("out : \(theNumber)")
}
}
// Table View delegates
extension ViewController: UITableViewDataSource, UITableViewDelegate
{
//One row
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
// Load custom cell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("thisCustomCell", forIndexPath: indexPath) as! CellViewController
return cell
}
}
//-------------------- Protocol for Delegate -----------------------
protocol CellInfoDelegate {
func processThatNumber(theNumber: Int)
}
//-------------------- Cell to Pass info to Parent -----------------------
class CellViewController: UITableViewCell{
var sumNumber: Int = 0
var delegate: CellInfoDelegate?
@IBAction func addButton(sender: AnyObject) {
// increment that number
self.sumNumber += 5
//1) I want to get it from here...... but delegate ends up nil
if let delegate = self.delegate {
delegate.processThatNumber(self.sumNumber)
}
//Shows that the number is incrementing
println(sumNumber)
}
}
Run Code Online (Sandbox Code Playgroud)

ViewController和CellViewController连接到它们各自的类
提前致谢.
i_a*_*orf 13
你应该在这里设置代表:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("thisCustomCell", forIndexPath: indexPath) as! CellViewController
cell.delegate = self // <-- Set the delegate.
return cell
}
Run Code Online (Sandbox Code Playgroud)
感谢i_am_jorf的解决方案,这里有适用的代码.
//
// ViewController.swift
// TableCellDelegate
//
// Created by Chris Cantley on 6/1/15.
// Copyright (c) 2015 Chris Cantley. All rights reserved.
//
import UIKit
import Foundation
class ViewController: UIViewController, CellInfoDelegate {
//The place to put the number into.
@IBOutlet weak var sumLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
//2)...to here.
func processThatNumber(theNumber: Int) {
println("out : \(theNumber)")
self.sumLabel.text = toString(theNumber) as String
}
}
// Table View delegates
extension ViewController: UITableViewDataSource, UITableViewDelegate
{
//One row
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
// Load custom cell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("thisCustomCell", forIndexPath: indexPath) as! CellViewController
//SOLUTION : put the Delgate HERE in the place where the cell is instantiated so that there is a connection back
// to this class from the Cell class
cell.delegate = self
return cell
}
}
//-------------------- Protocol for Delegate -----------------------
protocol CellInfoDelegate {
func processThatNumber(theNumber: Int)
}
//-------------------- Cell to Pass info to Parent -----------------------
class CellViewController: UITableViewCell{
var sumNumber: Int = 0
var delegate: CellInfoDelegate?
@IBAction func addButton(sender: AnyObject) {
// increment that number
self.sumNumber += 5
//1) I want to get it from here...... but delegate ends up nil
if let delegate = self.delegate {
delegate.processThatNumber(self.sumNumber)
}
//Shows that the number is incrementing
println(sumNumber)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8142 次 |
| 最近记录: |