[英]Tableview won't reloadData()
Sup guys? 大家好 Need some help over here. 在这里需要一些帮助。 I used the delegate protocol to pass some strings back from a "second view controller" to it's previous one. 我使用委托协议将一些字符串从“第二个视图控制器”传递回它的前一个。
My array appends the string from the method I implemented on the delegate protocol, but everytime I press "add" and come back to the first screen, the table view doesn't change. 我的数组附加了我在委托协议上实现的方法中的字符串,但是每次我按“添加”并返回到第一个屏幕时,表视图都不会改变。 I'm not sure if the string is not being passed between views or if it's just the table view that isn't reloading. 我不确定字符串是否在视图之间传递,或者只是表视图没有重新加载。
The code is quite simple: 代码很简单:
import UIKit
class EsseVaiReceber: UIViewController, UITableViewDataSource, UITableViewDelegate, PassInfoDelegate {
@IBOutlet var listaDeCoisasAFazer: UITableView!
var arrayInfo : [String] = []
var stringReceived: String = ""
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
listaDeCoisasAFazer.reloadData()
print("SCREEN APPEARED")
}
override func viewDidLoad() {
super.viewDidLoad()
listaDeCoisasAFazer.reloadData()
print("LOADED SCREEN")
}
func passInfo(infothatwillpass: String) {
arrayInfo.append(infothatwillpass)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayInfo.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell!
let row = indexPath.row
let titulo = arrayInfo[row]
cell.textLabel!.text = titulo
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "add") {
let view = segue.destinationViewController as! EsseVaiPassar
view.delegate = self
}
}
}
and : 和:
import Foundation
import UIKit
protocol PassInfoDelegate {
func passInfo(infothatwillpass: String)
}
class EsseVaiPassar: UIViewController {
@IBOutlet var campoTitulo: UITextField!
var delegate: PassInfoDelegate?
var textinput: String = ""
@IBAction func botaoAdicionarCoisasAFazer(sender: AnyObject) {
if campoTitulo == nil { return }
textinput = campoTitulo!.text!
print("TEXTO NO CAMPO:\(textinput)")
if delegate == nil {
print("DELEGATE IS NILL")
return
}
delegate!.passInfo(textinput)
print("TEXTO DO DELEGATE: \(textinput)")
if let navigation = self.navigationController {
navigation.popViewControllerAnimated(true)
print("INFO QUE VAI PASSAR: \(textinput)")
}
}
}
Any ideas why the table view won't populate? 为何无法填充表格视图? Thanks advanced =) 感谢高级=)
The tableView is using arrayInfo
as the source for the listaDeCoisasAFazer
tableView data (indirectly). tableView使用arrayInfo
作为listaDeCoisasAFazer
tableView数据的源(间接)。 If you change arrayInfo
you need to tell listaDeCoisasAFazer
to reloadData()
. 如果更改arrayInfo
,则需要告诉listaDeCoisasAFazer
reloadData()
。
func passInfo(infothatwillpass: String) {
arrayInfo.append(infothatwillpass)
}
You can/should remove the reloadData()
from viewWillLoad
and viewDidLoad
as the dataSource hasn't changed so there is no need to reload the data. 您可以/应该从viewWillLoad
和viewDidLoad
删除reloadData()
,因为dataSource尚未更改,因此无需重新加载数据。
TIP 小费
You are checking for nil
before the delegate
call in the destination viewController. 您要在目标viewController中的delegate
调用之前检查nil
。 Unless you are doing this for debugging purposes, you can simply call delegate?.passInfo(textinput)
. 除非您出于调试目的这样做,否则您可以简单地调用delegate?.passInfo(textinput)
。 If delegate is nil, it will just ignore the call. 如果委托为nil,它将忽略该调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.