[英]Pass arrays data from a ViewController to a TableView
I couldn't find this specific case where i have to pass and append two or more arrays of string into a TableView located in 2nd ViewController. 我找不到这种特殊的情况,我必须将两个或多个字符串数组传递并附加到位于第二个ViewController的TableView中。 I want to push two different strings inside two different UILabels
我想在两个不同的UILabel中推送两个不同的字符串
I have some data that gets inside through a UIAlertViewController in mainViewController 我有一些数据通过mainViewController中的UIAlertViewController进入
var name = alertController.textFields?[0].text
var number = alertController.textFields?[1].text
Then i take this data and every time i get new set of name/number i append it to arrays of string in MainVC 然后我获取这些数据,每次获取新的名称/数字集时,都会将其附加到MainVC中的字符串数组中
var playerNames = [String]()
var playerNumbers = [String]()
self.playerNames.append(name!)
self.playerNumbers.append(number!)
now i created a table view and a custom cell with .xib and i'd call it and set to custom labels like this 现在,我使用.xib创建了一个表格视图和一个自定义单元格,我将其命名并设置为这样的自定义标签
TableView in Second ViewController 第二个ViewController中的TableView
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->
UITableViewCell {
customCell.tableViewCellNumber.text = mainVC.playerNumbers[indexPath.row]
customCell.tableViewCellName.text = mainVC.playerNames[indexPath.row]
return customCell
}
So for every new set it makes a new row with name + number 因此,对于每个新集,它都会使用名称+数字创建一个新行
I tried anything from userDefaults to performForSegue but it doesnt work! 我尝试了任何从userDefaults到performForSegue的方法,但是它不起作用! I can print the arrays just fine, but it wont show me new rows at all.
我可以很好地打印数组,但是根本不会显示新行。
EDIT: actual question 编辑:实际问题
Ok I guess I misunderstood the use of NsUserDefaults... at the moment i have in viewDidLoad of my 2nd controller 好吧,我猜我误解了NsUserDefaults的使用...此刻我在第二个控制器的viewDidLoad中
if ((UserDefaults.standard.array(forKey: "playerNames")) != nil){
let playerNames = (UserDefaults.standard.array(forKey:
"playerNames") as? [String])!
print(playerNames)
}
}
and its actually printing the new values, now i want the new appended value to show in the custom cell label.text cell.nameLabel.text = The Value
how do i retrieve the value everytime and store it? 及其实际打印的新值,现在我希望新添加的值显示在自定义单元格label.text
cell.nameLabel.text = The Value
我如何每次都检索和存储该值? if i try with playerNames, it says it doesnt exist.. 如果我尝试使用playerNames,它说它不存在。
Did you synchronize user defaults after inputting data? 输入数据后是否同步了用户默认设置? UserDefaults.synchronize()
UserDefaults.synchronize()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.